Guest User

Untitled

a guest
Jan 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. # The 'lexer' macro generates not only the lexing proc but also
  2. # the relevant 'Token' type, including a TokenKind enum that is
  3. # extracted from the actions via the 'token Kind()' syntax.
  4. # There are two special TokenKind values always available:
  5. # Error and Eof.
  6. # '_' refers to the matched string.
  7.  
  8. lexer:
  9. "[A-Za-z][A-Za-z0-9]*":
  10. # 'keywords' is a special construct. We want to map the pattern
  11. # to the 'Identifier' token kind, unless it is a keyword. Every
  12. # keyword is mapped to its own TokenKind enum value.
  13. keywords("""
  14. if else for while case of
  15. """, token Identifier(_))
  16.  
  17. "[0-9]+": token Float(_)
  18. ",": token Comma()
  19. "+": token Plus()
  20. "{.": token PragmaLe()
  21. ".}": token PragmaRi()
  22. "{": token CurlyLe()
  23. "}": token CurlyRi()
  24. ".*": token SomethingElse(_)
  25.  
  26.  
  27. # Likewise the 'parser' macro generates a 'Tree' type, including a
  28. # NodeKind enum that is extracted from the actions via
  29. # the 'node Kind(...)' syntax.
  30. # There is one special NodeKind value always available: Error.
  31. # it[i] refers to the i-th parsed subtree. 'it' refers to all parsed
  32. # subtree as a seq, '_' to the current token.
  33.  
  34. parser:
  35. expr "expr ParLe expr *^ Comma ParRi":
  36. # 'a *^ sep' is a shortcut for (a (sep a)*)?
  37. node Call(it)
  38.  
  39. expr "Identifier":
  40. node Ident(_)
  41.  
  42. expr "expr Plus expr":
  43. node Add(it)
  44.  
  45. stmt "expr":
  46. discard "every expression is a statement"
  47. stmt "If expr Colon expr Else expr":
  48. node IfStmt(it)
  49. stmt "PragmaLe stmt (CurlyRi|PragmaRi)":
  50. # There is no 'or' operator beyond "token or" because it is not
  51. # clear what parse tree to attach to an 'or'.
  52. node Pragma(it[0])
Add Comment
Please, Sign In to add comment