Advertisement
ptrelford

Prototype C# Parser

Dec 31st, 2013
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.98 KB | None | 0 0
  1. #r @"..\packages\FParsec.1.0.1\lib\net40-client\FParsecCS.dll"
  2. #r @"..\packages\FParsec.1.0.1\lib\net40-client\FParsec.dll"
  3.  
  4. open FParsec
  5.  
  6. #load "AST.fs"
  7.  
  8. let ws = spaces
  9. let str_ws s = pstring s .>> ws
  10. let str_ws1 s = pstring s .>> spaces1
  11.  
  12. let pidentifier =
  13.     let isIdentifierFirstChar c = isLetter c || c = '_'
  14.     let isIdentifierChar c = isLetter c || isDigit c || c = '_'
  15.     many1Satisfy2L isIdentifierFirstChar isIdentifierChar "identifier"
  16. let pidentifier_ws = pidentifier .>> ws
  17. let pvar = pidentifier |>> (fun x -> Variable(x))
  18.  
  19. let pexpr = pvar
  20.  
  21. let ppublic = str_ws1 "public" |>> (fun _ -> Public)
  22. let pprivate = str_ws1 "private" |>> (fun _ -> Private)
  23. let pprotected = str_ws1 "protected" |>> (fun _ -> Protected)
  24. let pinternal = str_ws1 "internal" |>> (fun _ -> Internal)
  25. let paccess = ppublic <|> pprivate <|> pprotected <|> pinternal
  26. let psealed = str_ws1 "sealed" |>> (fun _ -> Sealed)
  27. let pstatic = str_ws1 "static" |>> (fun _ -> Static)
  28. let pmodifier = psealed <|> pstatic
  29.  
  30. let pmemberblock = between (str_ws "{") (str_ws "}") ws |>> (fun _ -> [])
  31.  
  32. let pclass =
  33.     pipe5 (opt paccess) (opt pmodifier) (str_ws1 "class") pidentifier_ws pmemberblock
  34.      (fun access modifier _ name block ->
  35.         let access = defaultArg access Internal
  36.         Class(access, modifier, name, block)
  37.      )
  38.  
  39. let pscope, pscopeimpl = createParserForwardedToRef()
  40.  
  41. let pscopesblock = between (str_ws "{") (str_ws "}") (many pscope) |>> (fun scopes -> scopes)
  42.  
  43. let pimport = str_ws1 "using" >>. pidentifier .>> str_ws ";" |>> (fun name -> Import(name))
  44. let pnsblock =
  45.     pipe3 (many pimport) (str_ws1 "namespace" >>. pidentifier_ws) pscopesblock
  46.      (fun imports name block ->
  47.         let types = Types([],[])
  48.         Namespace(imports,name,block))
  49. let pclasses =
  50.     pipe2 (many pimport) (many1 pclass)
  51.      (fun imports classes -> Types(imports, classes))
  52. pscopeimpl := pnsblock <|> (pclasses)
  53.  
  54. run pscope "using X; using Y; namespace A { namespace B { class A { } } }"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement