1 people like it.

Parse locale files

This uses fparsec to parse locale files of hte form Id = text {arg:type} = newline = newlne For use with localization.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
87: 
88: 
89: 
90: 
module FParsecCombinators

open FParsec
open System

exception Error of string

type Arg = 
    | WithType of string * string
    | NoType of string

type LocaleContents = 
    | Argument of Arg
    | Text of string
    | Line of LocaleContents list
    | Comment of string
    | NewLine

type Locale =
    | Entry of string * LocaleContents
    | IgnoreEntry of LocaleContents

(*
    Utilities
*)

let brackets = isAnyOf ['{';'}']
    
 (* non new line space *)  
let regSpace = manySatisfy (isAnyOf [' ';'\t'])
  
(* any string literal that is charcaters *)
let phrase = many1Chars (satisfy (isNoneOf ['{';'\n']))
  
let singleWord = many1Chars (satisfy isDigit <|> satisfy isLetter <|> satisfy (isAnyOf ['_';'-']))

(* utility method to set between parsers space agnostic *)
let between x y p = pstring x >>. regSpace >>. p .>> regSpace .>> pstring y

(*
    Arguments
*)

let argDelim = pstring ":"

let argumentNoType = singleWord |>> NoType

let argumentWithType = singleWord .>>.? (argDelim >>. singleWord) |>> WithType

let arg = (argumentWithType <|> argumentNoType) |> between "{" "}" |>> Argument

(*
    Text Elements
*)

let textElement = phrase |>> Text

let newLine = (unicodeNewline >>? regSpace >>? pstring "=") >>% NewLine

let line = many (regSpace >>? (arg <|> textElement <|> newLine)) |>> Line

(*
    Entries
*)

let delim = regSpace >>. pstring "=" .>> regSpace

let identifier = regSpace >>. singleWord .>> delim

let localeElement = unicodeSpaces >>? (identifier .>>. line .>> skipRestOfLine true) |>> Entry

(*
    Comments
*)

let comment = pstring "#" >>. restOfLine false |>> Comment

let commentElement = unicodeSpaces >>? comment |>> IgnoreEntry

(*
    Full Locale
*)

let locale = many (commentElement <|> localeElement) .>> eof

let test input = match run locale input with
                    | Success(r,_,_) -> r
                    | Failure(r,_,_) -> 
                            Console.WriteLine r
                            raise (Error(r))
module FParsecCombinators
namespace FParsec
namespace System
Multiple items
exception Error of string

Full name: FParsecCombinators.Error

--------------------
module Error

from FParsec
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
type Arg =
  | WithType of string * string
  | NoType of string

Full name: FParsecCombinators.Arg
union case Arg.WithType: string * string -> Arg
union case Arg.NoType: string -> Arg
type LocaleContents =
  | Argument of Arg
  | Text of string
  | Line of LocaleContents list
  | Comment of string
  | NewLine

Full name: FParsecCombinators.LocaleContents
union case LocaleContents.Argument: Arg -> LocaleContents
Multiple items
union case LocaleContents.Text: string -> LocaleContents

--------------------
type Text =
  static member CountTextElements : str:string -> int
  static member FoldCase : str:string -> string + 1 overload
  static member IsHighSurrogate : ch:char -> bool
  static member IsLowSurrogate : ch:char -> bool
  static member IsSurrogate : ch:char -> bool
  static member IsWhitespace : ch:char -> bool
  static member NormalizeNewlines : str:string -> string

Full name: FParsec.Text
union case LocaleContents.Line: LocaleContents list -> LocaleContents
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
union case LocaleContents.Comment: string -> LocaleContents
union case LocaleContents.NewLine: LocaleContents
type Locale =
  | Entry of string * LocaleContents
  | IgnoreEntry of LocaleContents

Full name: FParsecCombinators.Locale
union case Locale.Entry: string * LocaleContents -> Locale
union case Locale.IgnoreEntry: LocaleContents -> Locale
val brackets : (char -> bool)

Full name: FParsecCombinators.brackets
val isAnyOf : seq<char> -> (char -> bool)

Full name: FParsec.CharParsers.isAnyOf
val regSpace : Parser<string,unit>

Full name: FParsecCombinators.regSpace
val manySatisfy : (char -> bool) -> Parser<string,'u>

Full name: FParsec.CharParsers.manySatisfy
val phrase : Parser<string,unit>

Full name: FParsecCombinators.phrase
val many1Chars : Parser<char,'u> -> Parser<string,'u>

Full name: FParsec.CharParsers.many1Chars
val satisfy : (char -> bool) -> Parser<char,'u>

Full name: FParsec.CharParsers.satisfy
val isNoneOf : seq<char> -> (char -> bool)

Full name: FParsec.CharParsers.isNoneOf
val singleWord : Parser<string,unit>

Full name: FParsecCombinators.singleWord
val isDigit : char -> bool

Full name: FParsec.CharParsers.isDigit
val isLetter : char -> bool

Full name: FParsec.CharParsers.isLetter
val between : x:string -> y:string -> p:Parser<'a,unit> -> Parser<'a,unit>

Full name: FParsecCombinators.between
val x : string
val y : string
val p : Parser<'a,unit>
val pstring : string -> Parser<string,'u>

Full name: FParsec.CharParsers.pstring
val argDelim : Parser<string,unit>

Full name: FParsecCombinators.argDelim
val argumentNoType : Parser<Arg,unit>

Full name: FParsecCombinators.argumentNoType
val argumentWithType : Parser<Arg,unit>

Full name: FParsecCombinators.argumentWithType
val arg : Parser<LocaleContents,unit>

Full name: FParsecCombinators.arg
val textElement : Parser<LocaleContents,unit>

Full name: FParsecCombinators.textElement
val newLine : Parser<LocaleContents,unit>

Full name: FParsecCombinators.newLine
val unicodeNewline<'u> : Parser<char,'u>

Full name: FParsec.CharParsers.unicodeNewline
val line : Parser<LocaleContents,unit>

Full name: FParsecCombinators.line
val many : Parser<'a,'u> -> Parser<'a list,'u>

Full name: FParsec.Primitives.many
val delim : Parser<string,unit>

Full name: FParsecCombinators.delim
val identifier : Parser<string,unit>

Full name: FParsecCombinators.identifier
val localeElement : Parser<Locale,unit>

Full name: FParsecCombinators.localeElement
val unicodeSpaces : Parser<unit,'u>

Full name: FParsec.CharParsers.unicodeSpaces
val skipRestOfLine : bool -> Parser<unit,'u>

Full name: FParsec.CharParsers.skipRestOfLine
val comment : Parser<LocaleContents,unit>

Full name: FParsecCombinators.comment
val restOfLine : bool -> Parser<string,'u>

Full name: FParsec.CharParsers.restOfLine
val commentElement : Parser<Locale,unit>

Full name: FParsecCombinators.commentElement
val locale : Parser<Locale list,unit>

Full name: FParsecCombinators.locale
val eof : Parser<unit,'u>

Full name: FParsec.CharParsers.eof
val test : input:string -> Locale list

Full name: FParsecCombinators.test
val input : string
val run : Parser<'Result,unit> -> string -> ParserResult<'Result,unit>

Full name: FParsec.CharParsers.run
union case ParserResult.Success: 'Result * 'UserState * Position -> ParserResult<'Result,'UserState>
val r : Locale list
union case ParserResult.Failure: string * ParserError * 'UserState -> ParserResult<'Result,'UserState>
val r : string
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.WriteLine() : unit
   (+0 other overloads)
Console.WriteLine(value: string) : unit
   (+0 other overloads)
Console.WriteLine(value: obj) : unit
   (+0 other overloads)
Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
Console.WriteLine(value: int64) : unit
   (+0 other overloads)
Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
Console.WriteLine(value: int) : unit
   (+0 other overloads)
Console.WriteLine(value: float32) : unit
   (+0 other overloads)
Console.WriteLine(value: float) : unit
   (+0 other overloads)
Console.WriteLine(value: decimal) : unit
   (+0 other overloads)
val raise : exn:Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
Raw view New version

More information

Link:http://fssnip.net/iG
Posted:3 years ago
Author:devshorts
Tags: locale , parsing , fparsec