parser.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package muesli
  2. import (
  3. _ "bufio"
  4. _ "bytes"
  5. _ "errors"
  6. "fmt"
  7. _ "io"
  8. _ "os"
  9. _ "reflect"
  10. _ "runtime"
  11. _ "strings"
  12. _ "unicode"
  13. // "gitlab.com/beoran/woe/graphviz"
  14. // _ "gitlab.com/beoran/woe/monolog"
  15. )
  16. /* Grammar:
  17. Desrired syntax (verified LL(1) on smlweb.cpsc.ucalgary.ca)
  18. PROGRAM -> STATEMENTS.
  19. STATEMENTS -> STATEMENT STATEMENTS | .
  20. STATEMENT -> EXPRESSION eos | BLOCK .
  21. WORDOPS -> WORDOP WORDOPS | .
  22. EXPRESSION -> SETTER | GETTER | COMMAND | VALUE.
  23. COMMAND -> WORDVALUE PARAMETERS.
  24. PARAMETERS -> PARAMETER PARAMETERS | .
  25. PARAMETER -> WORDVALUE | PARENTHESIS | GETTER | ARRARY | BLOCK .
  26. PARENTHESIS -> '(' EXPRESSION ')' .
  27. BLOCK -> '{' STATEMENTS '}' .
  28. WORDVALUE -> word | VALUE.
  29. VALUE -> string | integer | float | symbol | boolean | nil.
  30. SETTER -> set word PARAMETERS .
  31. GETTER -> get word .
  32. *
  33. * program -> statements
  34. * statements -> statement+
  35. * statement -> get / set / command
  36. *
  37. */
  38. type Parser struct {
  39. Lexer *Lexer
  40. current Token
  41. LoggerWrapper
  42. }
  43. func (parser *Parser) SetLogger(logger Logger) {
  44. parser.LoggerWrapper = LoggerWrapper{logger}
  45. }
  46. func (parser *Parser) Advance() {
  47. token := parser.Lexer.Lex()
  48. parser.current = token
  49. parser.LogDebug("Next token: %s\n", token.String())
  50. }
  51. /* Looks at the current token and advances the lexer if the token is of any of
  52. the token kinds given in kinds. In this case it will return the accepted
  53. token and advance the parser. Otherwise, if no token kind matches, the lexer
  54. does not advance and the current token remains the same, except if that was nil.*/
  55. func (parser *Parser) Accept(kinds ...TokenKind) Token {
  56. if parser.current.IsNone() {
  57. parser.Advance()
  58. }
  59. for _, kind := range kinds {
  60. if kind == parser.current.TokenKind {
  61. accepted := parser.current
  62. parser.Advance()
  63. return accepted
  64. }
  65. }
  66. return NoToken()
  67. }
  68. func (parser *Parser) ParseMany(kind AstKind, parent *Ast, parsefunc func(*Parser) Ast) Ast {
  69. children := make([]Ast, 0)
  70. for sub := parsefunc(parser); !sub.IsNone() && !sub.IsError(); sub = parsefunc(parser) {
  71. children = append(children, sub)
  72. }
  73. return NewAst(kind, parent, children, NoToken())
  74. }
  75. func (parser *Parser) NewAstError(message string, args ...interface{}) Ast {
  76. sv := StringValue(fmt.Sprintf(message+" at token "+parser.current.String(), args))
  77. pos := parser.current.Position
  78. tok := NewToken(TokenKindError, sv, pos)
  79. return NewAst(AstKindError, nil, EmptyAstArray(), tok)
  80. }
  81. func (parser *Parser) ParseAny(astkind AstKind, parsefuncs ...(func(*Parser) Ast)) Ast {
  82. ast := NewEmptyAst(astkind)
  83. for _, parsefunc := range parsefuncs {
  84. parser.LogDebug("ParseAny: trying: %s", GetFunctionName(parsefunc))
  85. sub := parsefunc(parser)
  86. if !sub.IsNone() {
  87. ast.AppendChild(sub)
  88. return ast
  89. }
  90. }
  91. return NewAstNone()
  92. }
  93. func (parser *Parser) ParseRequireAny(astkind AstKind, parsefuncs ...(func(*Parser) Ast)) Ast {
  94. ast := parser.ParseAny(astkind, parsefuncs...)
  95. if ast.IsNone() {
  96. err := parser.NewAstError("Unexpected token in %v", parsefuncs)
  97. return err
  98. }
  99. return ast
  100. }
  101. func (parser *Parser) ParseValue() Ast {
  102. value := parser.Accept(TokenKindInteger, TokenKindString,
  103. TokenKindBoolean, TokenKindNil, TokenKindFloat, TokenKindSymbol)
  104. if value.IsNone() {
  105. return NewAstNone()
  106. }
  107. return NewAst(AstKindValue, nil, EmptyAstArray(), value)
  108. }
  109. func (parser *Parser) ParseWordValue() Ast {
  110. value := parser.Accept(TokenKindInteger, TokenKindString,
  111. TokenKindBoolean, TokenKindFloat, TokenKindSymbol,
  112. TokenKindType, TokenKindWord)
  113. if value.IsNone() {
  114. return NewAstNone()
  115. }
  116. return NewAst(AstKindWordValue, nil, EmptyAstArray(), value)
  117. }
  118. func (parser *Parser) ParseArgument() Ast {
  119. return parser.ParseAny(AstKindArgument,
  120. (*Parser).ParseWordValue,
  121. (*Parser).ParseGet,
  122. (*Parser).ParseSet,
  123. (*Parser).ParseParenthesis,
  124. (*Parser).ParseList,
  125. (*Parser).ParseBlock)
  126. }
  127. func (parser *Parser) ParseArguments() Ast {
  128. return parser.ParseMany(AstKindArguments, nil, (*Parser).ParseArgument)
  129. }
  130. func (parser *Parser) ParseList() Ast {
  131. op := parser.Accept(TokenKindOpenList)
  132. if op.IsNone() {
  133. return NewAstNone()
  134. }
  135. ast := NewAstWithToken(AstKindList, op)
  136. args := parser.ParseArguments()
  137. if AstIsError(args) {
  138. return args
  139. }
  140. if cp := parser.Accept(TokenKindCloseList); cp.IsNone() {
  141. return parser.NewAstError("expected closing brackets")
  142. }
  143. ast.AppendChild(args)
  144. return ast
  145. }
  146. func (parser *Parser) ParseParenthesis() Ast {
  147. op := parser.Accept(TokenKindOpenParen)
  148. if op.IsNone() {
  149. return NewAstNone()
  150. }
  151. ast := NewAstWithToken(AstKindParenthesis, op)
  152. expr := parser.ParseExpression()
  153. if expr.IsNone() {
  154. return parser.NewAstError("expected expression")
  155. }
  156. if AstIsError(expr) {
  157. return expr
  158. }
  159. if cp := parser.Accept(TokenKindCloseParen); cp.IsNone() {
  160. return parser.NewAstError("expected closing parenthesis")
  161. }
  162. ast.AppendChild(expr)
  163. return ast
  164. }
  165. func (parser *Parser) ParseBlock() Ast {
  166. op := parser.Accept(TokenKindOpenBlock)
  167. if op.IsNone() {
  168. return NewAstNone()
  169. }
  170. ast := NewAstWithToken(AstKindBlock, op)
  171. stats := parser.ParseStatements()
  172. if stats.IsNone() {
  173. return parser.NewAstError("expected expression")
  174. }
  175. if AstIsError(stats) {
  176. return stats
  177. }
  178. if cp := parser.Accept(TokenKindCloseBlock); cp.IsNone() {
  179. return parser.NewAstError("expected closing block")
  180. }
  181. ast.AppendChild(stats)
  182. return ast
  183. }
  184. /* Parses the target of a set or get expression */
  185. func (parser *Parser) ParseTarget() Ast {
  186. target := parser.Accept(TokenKindWord, TokenKindType, TokenKindSymbol)
  187. ast := NewAstWithToken(AstKindTarget, target)
  188. if target.IsNone() {
  189. paren := parser.ParseParenthesis()
  190. if paren.IsNone() {
  191. return parser.NewAstError("expected word, symbol or parenthesis")
  192. }
  193. ast.AppendChild(paren)
  194. }
  195. return ast
  196. }
  197. func (parser *Parser) ParseSet() Ast {
  198. set := parser.Accept(TokenKindSet)
  199. if set.IsNone() {
  200. return NewAstNone()
  201. }
  202. ast := NewAstWithToken(AstKindSet, set)
  203. target := parser.ParseTarget()
  204. ast.AppendChild(target)
  205. argument := parser.ParseArgument()
  206. if argument.IsNone() {
  207. return parser.NewAstError("Expected argument to set")
  208. }
  209. ast.AppendChild(argument)
  210. return ast
  211. }
  212. func (parser *Parser) ParseGet() Ast {
  213. get := parser.Accept(TokenKindGet)
  214. if get.IsNone() {
  215. return NewAstNone()
  216. }
  217. ast := NewAstWithToken(AstKindGet, get)
  218. target := parser.ParseTarget()
  219. ast.AppendChild(target)
  220. return ast
  221. }
  222. func (parser *Parser) ParseCommand() Ast {
  223. word := parser.Accept(TokenKindWord, TokenKindType)
  224. if word.IsNone() {
  225. return NewAstNone()
  226. }
  227. arguments := parser.ParseArguments()
  228. command := NewAstWithToken(AstKindCommand, word)
  229. command.AppendChild(arguments)
  230. return command
  231. }
  232. func (parser *Parser) ParseExpression() Ast {
  233. exp := parser.ParseRequireAny(AstKindExpression, (*Parser).ParseSet,
  234. (*Parser).ParseGet, (*Parser).ParseCommand, (*Parser).ParseValue )
  235. return exp
  236. }
  237. func (parser *Parser) ParseEmptyStatement() Ast {
  238. eox := parser.Accept(TokenKindEOX)
  239. if eox.IsNone() {
  240. return NewAstNone()
  241. }
  242. ast := NewAstWithToken(AstKindEnd, eox)
  243. return ast
  244. }
  245. func (parser *Parser) ParseStatement() Ast {
  246. ast := parser.ParseRequireAny(AstKindStatement,
  247. (*Parser).ParseBlock,
  248. (*Parser).ParseSet,
  249. (*Parser).ParseGet,
  250. (*Parser).ParseCommand,
  251. (*Parser).ParseParenthesis)
  252. if eox := parser.Accept(TokenKindEOX); eox.IsNone() {
  253. return parser.NewAstError("expected end of statement")
  254. }
  255. return ast
  256. }
  257. func (parser *Parser) ParseStatements() Ast {
  258. return parser.ParseMany(AstKindStatements, nil, (*Parser).ParseStatement)
  259. }
  260. func (parser *Parser) ParseProgram() Ast {
  261. eof := parser.Accept(TokenKindEOF)
  262. aeof := NewAstWithToken(AstKindEnd, eof)
  263. stats := parser.ParseStatements()
  264. // Be lenient with missing EOF for now...
  265. children := []Ast{stats, aeof}
  266. return NewAst(AstKindProgram, nil, children, NoToken())
  267. }
  268. func (parser *Parser) Parse() Ast {
  269. ast := parser.ParseProgram()
  270. return ast
  271. }
  272. func NewParser(lexer *Lexer) *Parser {
  273. parser := &Parser{lexer, NoToken(), LoggerWrapper{nil}}
  274. return parser
  275. }
  276. func NewParserFromString(input string) *Parser {
  277. lexer := NewLexerFromString(input)
  278. return NewParser(lexer)
  279. }
  280. func NewParserFromFilename(filename string) (*Parser, error) {
  281. lexer, err := NewLexerFromFilename(filename)
  282. if err != nil {
  283. return nil, err
  284. }
  285. return NewParser(lexer), nil
  286. }