Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #r @"..\packages\FParsec.1.0.1\lib\net40-client\FParsecCS.dll"
- #r @"..\packages\FParsec.1.0.1\lib\net40-client\FParsec.dll"
- open FParsec
- #load "AST.fs"
- let ws = spaces
- let str_ws s = pstring s .>> ws
- let str_ws1 s = pstring s .>> spaces1
- let pidentifier =
- let isIdentifierFirstChar c = isLetter c || c = '_'
- let isIdentifierChar c = isLetter c || isDigit c || c = '_'
- many1Satisfy2L isIdentifierFirstChar isIdentifierChar "identifier"
- let pidentifier_ws = pidentifier .>> ws
- let pvar = pidentifier |>> (fun x -> Variable(x))
- let pexpr = pvar
- let ppublic = str_ws1 "public" |>> (fun _ -> Public)
- let pprivate = str_ws1 "private" |>> (fun _ -> Private)
- let pprotected = str_ws1 "protected" |>> (fun _ -> Protected)
- let pinternal = str_ws1 "internal" |>> (fun _ -> Internal)
- let paccess = ppublic <|> pprivate <|> pprotected <|> pinternal
- let psealed = str_ws1 "sealed" |>> (fun _ -> Sealed)
- let pstatic = str_ws1 "static" |>> (fun _ -> Static)
- let pmodifier = psealed <|> pstatic
- let pmemberblock = between (str_ws "{") (str_ws "}") ws |>> (fun _ -> [])
- let pclass =
- pipe5 (opt paccess) (opt pmodifier) (str_ws1 "class") pidentifier_ws pmemberblock
- (fun access modifier _ name block ->
- let access = defaultArg access Internal
- Class(access, modifier, name, block)
- )
- let pscope, pscopeimpl = createParserForwardedToRef()
- let pscopesblock = between (str_ws "{") (str_ws "}") (many pscope) |>> (fun scopes -> scopes)
- let pimport = str_ws1 "using" >>. pidentifier .>> str_ws ";" |>> (fun name -> Import(name))
- let pnsblock =
- pipe3 (many pimport) (str_ws1 "namespace" >>. pidentifier_ws) pscopesblock
- (fun imports name block ->
- let types = Types([],[])
- Namespace(imports,name,block))
- let pclasses =
- pipe2 (many pimport) (many1 pclass)
- (fun imports classes -> Types(imports, classes))
- pscopeimpl := pnsblock <|> (pclasses)
- run pscope "using X; using Y; namespace A { namespace B { class A { } } }"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement