parser.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. CLOSED -> BLOCK | LIST | PARENTHESIS .
  20. STATEMENTS -> STATEMENT STATEMENTS | .
  21. STATEMENT -> CLOSED | EXPRESSION eos | eos .
  22. COMMAND -> word PARAMETERS.
  23. PARAMETERS -> PARAMETER PARAMETERS | .
  24. PARAMETER -> WORDVALUE | GETTER | SETTER | CLOSED .
  25. EXPRESSION -> COMMAND | GETTER | SETTER | VALUE.
  26. PARENTHESIS -> closeparen EXPRESSION openparen .
  27. BLOCK -> openblock STATEMENTS closeblock .
  28. LIST -> openlist PARAMETERS closelist .
  29. VALUE -> string | int | float | symbol | type .
  30. WORDVALUE -> word | VALUE.
  31. SETTER -> set TARGET .
  32. TARGET -> word PARAMETER | GETTER PARAMETER .
  33. GETTER -> get ORIGIN .
  34. ORIGIN -> word | SETTER | GETTER .
  35. *
  36. * program -> statements
  37. * statements -> statement+
  38. * statement -> get / set / command
  39. *
  40. */
  41. type Parser struct {
  42. Lexer *Lexer
  43. current Token
  44. LoggerWrapper
  45. Errors []ParserError
  46. }
  47. // panic with this type on errors that would prevent the parser
  48. // from making progress.
  49. type ParserError struct {
  50. *Parser
  51. *Token
  52. Chain error
  53. }
  54. func (pe ParserError) Error() string {
  55. return fmt.Sprintf("%s %s", pe.Token.String(), pe.Chain.Error())
  56. }
  57. func (parser *Parser) SetLogger(logger Logger) {
  58. parser.LoggerWrapper = LoggerWrapper{logger}
  59. }
  60. func (parser *Parser) Errorf(message string, args ...interface{}) ParserError {
  61. err := fmt.Errorf(message, args...)
  62. pe := ParserError { Parser: parser, Token:&parser.current, Chain: err }
  63. parser.Errors = append(parser.Errors, pe)
  64. return pe
  65. }
  66. func (parser *Parser) Panicf(message string, args ...interface{}) {
  67. pe := parser.Errorf(message, args...)
  68. panic(pe)
  69. }
  70. func (parser *Parser) Advance() {
  71. token := parser.Lexer.Lex()
  72. parser.current = token
  73. parser.LogDebug("Next token: %s\n", token.String())
  74. }
  75. /* Skips tokens until a closer, namely ), }, ], EOX or EOF is found,
  76. * to skip parse errors, and to be able to continue parsing despite errors. */
  77. func (parser *Parser) SkipError() Token {
  78. parser.Advance()
  79. for {
  80. parser.Advance()
  81. if parser.NextIsErrorSkipped() {
  82. return parser.current
  83. }
  84. if parser.NextIs(TokenKindError, TokenKindNone) {
  85. parser.Panicf("Cannot recover from parse error. Parse ended.")
  86. }
  87. }
  88. return parser.current
  89. }
  90. /* Looks at the current token and advances the lexer if the token is of any of
  91. the token kinds given in kinds. In this case it will return the accepted
  92. token and advance the parser. Otherwise, it will call parser.Panicf with a
  93. * "Syntax error unexpected <token>".*/
  94. func (parser *Parser) Require(kinds ...TokenKind) Token {
  95. if parser.current.IsNone() {
  96. parser.Advance()
  97. }
  98. expected := ""
  99. sep := ""
  100. for _, kind := range kinds {
  101. if kind == parser.current.TokenKind {
  102. accepted := parser.current
  103. parser.Advance()
  104. parser.LogDebug("Require: Accepted token: %s\n", accepted.String())
  105. return accepted
  106. }
  107. expected = fmt.Sprintf("%s%s%s", expected, sep, kind.String())
  108. }
  109. parser.Panicf("error: expected one of the following: %s", expected)
  110. return Token{}
  111. }
  112. func (parser *Parser) NewAstError(message string, args ...interface{}) *Ast {
  113. sv := StringValue(fmt.Sprintf(message+" at token "+parser.current.String(), args))
  114. pos := parser.current.Position
  115. tok := NewToken(TokenKindError, sv, pos)
  116. parser.Errorf(message, args...)
  117. return NewAst(AstKindError, nil, EmptyAstArray(), tok)
  118. }
  119. // Also handles none or error vales of the token
  120. func (parser *Parser) NewAst(kind AstKind, parent * Ast, children []*Ast, value Token) *Ast{
  121. if value.IsNone() {
  122. return NewAstNone()
  123. }
  124. if value.IsError() {
  125. return NewAst(AstKindError, parent, children, value)
  126. }
  127. return NewAst(kind, parent, children, value)
  128. }
  129. func (parser *Parser) ParseValue() *Ast {
  130. parser.LogDebug("ParseValue: %s\n", parser.current.String())
  131. value := parser.Require(TokenKindInteger, TokenKindString,
  132. TokenKindBoolean, TokenKindNil, TokenKindFloat, TokenKindSymbol)
  133. return parser.NewAst(AstKindValue, nil, EmptyAstArray(), value)
  134. }
  135. func AstKindForToken(token Token) AstKind {
  136. switch token.TokenKind {
  137. case TokenKindInteger, TokenKindString, TokenKindBoolean,
  138. TokenKindNil, TokenKindFloat, TokenKindSymbol:
  139. return AstKindValue
  140. case TokenKindWord:
  141. return AstKindWord
  142. case TokenKindType:
  143. return AstKindType
  144. default:
  145. return AstKindError
  146. }
  147. }
  148. func (parser *Parser) ParseWordValue() *Ast {
  149. parser.LogDebug("ParseWordValue: %s\n", parser.current.String())
  150. value := parser.Require(TokenKindInteger, TokenKindString,
  151. TokenKindBoolean, TokenKindNil, TokenKindFloat, TokenKindSymbol,
  152. TokenKindType, TokenKindWord)
  153. astKind := AstKindForToken(value)
  154. return parser.NewAst(astKind, nil, EmptyAstArray(), value)
  155. }
  156. func (parser *Parser) ParseArgument() *Ast {
  157. parser.LogDebug("ParseArgument: %s\n", parser.current.String())
  158. switch {
  159. case parser.NextIsGet(): return parser.ParseGet()
  160. case parser.NextIsSet(): return parser.ParseSet()
  161. case parser.NextIsClosed(): return parser.ParseClosed()
  162. case parser.NextIsWordValue(): return parser.ParseWordValue()
  163. default: parser.Panicf("error: in argument: expected $, =, }, word or value")
  164. return nil
  165. }
  166. }
  167. func (parser *Parser) ParseArguments() *Ast {
  168. parser.LogDebug("ParseArguments: %s\n", parser.current.String())
  169. ast := NewAstWithToken(AstKindArguments, parser.current)
  170. var children []*Ast
  171. for parser.NextIsArgument() {
  172. child := parser.ParseArgument()
  173. children = append(children, child)
  174. }
  175. ast.AppendChildren(children...)
  176. return ast
  177. }
  178. func (parser *Parser) ParseList() *Ast {
  179. parser.LogDebug("ParseList: %s\n", parser.current.String())
  180. op := parser.Require(TokenKindOpenList)
  181. if op.IsError() {
  182. return parser.NewAstError("Unexpected value.")
  183. }
  184. ast := NewAstWithToken(AstKindList, op)
  185. args := parser.ParseArguments()
  186. if AstIsError(args) {
  187. return args
  188. }
  189. if cp := parser.Require(TokenKindCloseList); cp.IsError() {
  190. return parser.NewAstError("expected closing brackets")
  191. }
  192. ast.AppendChild(args)
  193. return ast
  194. }
  195. func (parser *Parser) ParseParenthesis() *Ast {
  196. parser.LogDebug("ParseParenthesis: %s\n", parser.current.String())
  197. op := parser.Require(TokenKindOpenParen)
  198. if op.IsError() {
  199. return parser.NewAstError("Unexpected value.")
  200. }
  201. ast := NewAstWithToken(AstKindParenthesis, op)
  202. expr := parser.ParseExpression()
  203. if expr.IsNone() {
  204. return parser.NewAstError("expected expression")
  205. }
  206. if AstIsError(expr) {
  207. return expr
  208. }
  209. if cp := parser.Require(TokenKindCloseParen); cp.IsError() {
  210. return parser.NewAstError("expected closing parenthesis")
  211. }
  212. ast.AppendChild(expr)
  213. return ast
  214. }
  215. func (parser *Parser) ParseBlock() *Ast {
  216. parser.LogDebug("ParseBlock: %s\n", parser.current.String())
  217. op := parser.Require(TokenKindOpenBlock)
  218. if op.IsError() {
  219. return parser.NewAstError("Unexpected value.")
  220. }
  221. ast := NewAstWithToken(AstKindBlock, op)
  222. stats := parser.ParseStatements()
  223. if stats.IsNone() {
  224. return parser.NewAstError("expected expression")
  225. }
  226. if AstIsError(stats) {
  227. return stats
  228. }
  229. if cp := parser.Require(TokenKindCloseBlock); cp.IsError() {
  230. return parser.NewAstError("expected closing block")
  231. }
  232. ast.AppendChild(stats)
  233. return ast
  234. }
  235. /* Parses the target of a set expression */
  236. func (parser *Parser) ParseTarget() *Ast {
  237. parser.LogDebug("ParseTarget: %s\n", parser.current.String())
  238. var target *Ast
  239. switch {
  240. case parser.NextIs(TokenKindWord, TokenKindType, TokenKindSymbol):
  241. // Direct target to a set
  242. token := parser.Require(TokenKindWord, TokenKindType, TokenKindSymbol)
  243. target = NewAstWithToken(AstKindTarget, token)
  244. case parser.NextIsGet():
  245. // Indirect target
  246. target = NewAstWithToken(AstKindTarget, parser.current)
  247. target.AppendChild(parser.ParseGet())
  248. case parser.NextIsClosed():
  249. // Indirect target
  250. target = NewAstWithToken(AstKindTarget, parser.current)
  251. target.AppendChild(parser.ParseClosed())
  252. default:
  253. parser.Panicf("Malformed setter.")
  254. return nil
  255. }
  256. argument := parser.ParseArgument()
  257. if argument.IsNone() {
  258. return parser.NewAstError("Expected argument to set")
  259. }
  260. target.AppendChild(argument)
  261. return target
  262. }
  263. /* Parses the origin of a get expression */
  264. func (parser *Parser) ParseOrigin() *Ast {
  265. parser.LogDebug("ParseOrigin: %s\n", parser.current.String())
  266. var target *Ast
  267. switch {
  268. case parser.NextIs(TokenKindWord, TokenKindType, TokenKindSymbol):
  269. // Direct target to a set
  270. token := parser.Require(TokenKindWord, TokenKindType, TokenKindSymbol)
  271. target = NewAstWithToken(AstKindTarget, token)
  272. case parser.NextIsGet():
  273. // Indirect target
  274. target = NewAstWithToken(AstKindTarget, parser.current)
  275. target.AppendChild(parser.ParseGet())
  276. case parser.NextIsClosed():
  277. // Indirect target
  278. target = NewAstWithToken(AstKindTarget, parser.current)
  279. target.AppendChild(parser.ParseClosed())
  280. default:
  281. parser.Panicf("Malformed getter")
  282. return nil
  283. }
  284. return target
  285. }
  286. func (parser *Parser) ParseSet() *Ast {
  287. parser.LogDebug("ParseSet: %s\n", parser.current.String())
  288. set := parser.Require(TokenKindSet)
  289. ast := NewAstWithToken(AstKindSet, set)
  290. target := parser.ParseTarget()
  291. ast.AppendChild(target)
  292. return ast
  293. }
  294. func (parser *Parser) ParseGet() *Ast {
  295. parser.LogDebug("ParseGet: %s\n", parser.current.String())
  296. get := parser.Require(TokenKindGet)
  297. ast := NewAstWithToken(AstKindGet, get)
  298. target := parser.ParseOrigin()
  299. ast.AppendChild(target)
  300. return ast
  301. }
  302. func (parser *Parser) ParseCommand() *Ast {
  303. parser.LogDebug("ParseCommand: %s\n", parser.current.String())
  304. word := parser.Require(TokenKindWord, TokenKindType)
  305. arguments := parser.ParseArguments()
  306. command := NewAstWithToken(AstKindCommand, word)
  307. command.AppendChild(arguments)
  308. return command
  309. }
  310. func (parser *Parser) ParseClosed() *Ast {
  311. parser.LogDebug("ParseClosed: %s\n", parser.current.String())
  312. switch {
  313. case parser.NextIs(TokenKindOpenBlock): return parser.ParseBlock()
  314. case parser.NextIs(TokenKindOpenParen): return parser.ParseParenthesis()
  315. case parser.NextIs(TokenKindOpenList): return parser.ParseList()
  316. default:
  317. parser.Panicf("Syntax error in closed, expected {, (, [")
  318. return nil
  319. }
  320. }
  321. func (parser *Parser) ParseExpression() *Ast {
  322. parser.LogDebug("ParseExpression: %s\n", parser.current.String())
  323. switch {
  324. case parser.NextIsWord(): return parser.ParseCommand()
  325. case parser.NextIsSet(): return parser.ParseSet()
  326. case parser.NextIsGet(): return parser.ParseGet()
  327. case parser.NextIsValue(): return parser.ParseValue()
  328. default:
  329. parser.Panicf("Syntax error in expression, expected word, $, =, value")
  330. return nil
  331. }
  332. }
  333. func (parser *Parser) ParseExpressionStatement() *Ast {
  334. parser.LogDebug("ParseExpressionStatement: %s\n", parser.current.String())
  335. expr := parser.ParseExpression()
  336. if expr.IsNone() {
  337. return NewAstNone()
  338. }
  339. // Expression statements must end on EOX or EOF
  340. if eox := parser.Require(TokenKindEOX, TokenKindEOF); eox.IsError() {
  341. return parser.NewAstError("expected end of statement")
  342. }
  343. return expr
  344. }
  345. func (parser *Parser) ParseEmptyStatement() *Ast {
  346. parser.LogDebug("ParseEmptyStatement: %s\n", parser.current.String())
  347. if eox := parser.Require(TokenKindEOX, TokenKindEOF); eox.IsError() {
  348. return parser.NewAstError("expected end of statement")
  349. }
  350. return NewAstWithToken(AstKindStatement, parser.current)
  351. }
  352. func (parser Parser) NextIs(kinds ...TokenKind) bool {
  353. if (parser.current.TokenKind == TokenKindNone) {
  354. parser.Advance()
  355. }
  356. for _, kind := range kinds {
  357. if kind == parser.current.TokenKind {
  358. return true
  359. }
  360. }
  361. return false
  362. }
  363. func (parser Parser) NextIsClosed() bool {
  364. return parser.NextIs(TokenKindOpenBlock, TokenKindOpenList, TokenKindOpenParen)
  365. }
  366. func (parser Parser) NextIsWord() bool {
  367. return parser.NextIs(TokenKindWord)
  368. }
  369. func (parser Parser) NextIsGet() bool {
  370. return parser.NextIs(TokenKindGet)
  371. }
  372. func (parser Parser) NextIsSet() bool {
  373. return parser.NextIs(TokenKindSet)
  374. }
  375. func (parser Parser) NextIsValue() bool {
  376. return parser.NextIs(TokenKindString, TokenKindType,
  377. TokenKindInteger, TokenKindFloat, TokenKindBoolean, TokenKindNil,
  378. TokenKindSymbol)
  379. }
  380. func (parser Parser) NextIsWordValue() bool {
  381. return parser.NextIs(TokenKindWord, TokenKindString, TokenKindType,
  382. TokenKindInteger, TokenKindFloat, TokenKindBoolean, TokenKindNil,
  383. TokenKindSymbol)
  384. }
  385. func (parser Parser) NextIsArgument() bool {
  386. return parser.NextIs(TokenKindOpenBlock, TokenKindOpenList, TokenKindOpenParen,
  387. TokenKindSet, TokenKindGet, TokenKindWord, TokenKindString, TokenKindType,
  388. TokenKindInteger, TokenKindFloat, TokenKindBoolean, TokenKindNil,
  389. TokenKindSymbol)
  390. }
  391. func (parser Parser) NextIsErrorSkipped() bool {
  392. return parser.NextIs(TokenKindCloseBlock, TokenKindCloseList, TokenKindCloseParen,
  393. TokenKindEOX, TokenKindEOF)
  394. }
  395. func (parser Parser) NextIsEOX() bool {
  396. return parser.NextIs(TokenKindEOX)
  397. }
  398. func (parser Parser) NextIsEOF() bool {
  399. return parser.NextIs(TokenKindEOF)
  400. }
  401. func (parser Parser) NextIsExpression() bool {
  402. return parser.NextIs(TokenKindWord,
  403. TokenKindGet,
  404. TokenKindSet,
  405. TokenKindString,
  406. TokenKindType,
  407. TokenKindInteger,
  408. TokenKindFloat,
  409. TokenKindBoolean, TokenKindNil,
  410. TokenKindSymbol)
  411. }
  412. func (parser Parser) NextIsStatement() bool {
  413. return parser.NextIsExpression() || parser.NextIsClosed() || parser.NextIsEOX()
  414. }
  415. func (parser *Parser) ParseStatement() *Ast {
  416. parser.LogDebug("ParseStatement: %s\n", parser.current.String())
  417. switch {
  418. case parser.NextIsClosed(): return parser.ParseClosed()
  419. case parser.NextIsExpression(): return parser.ParseExpressionStatement()
  420. case parser.NextIsEOX(): return parser.ParseEmptyStatement()
  421. default:
  422. parser.Panicf("Expected closed, expression, or EOX in statement")
  423. return parser.NewAstError("Expected closed, expression, or EOX in statement")
  424. }
  425. }
  426. func (parser *Parser) ParseStatements() *Ast {
  427. parser.LogDebug("ParseStatements: %s\n", parser.current.String())
  428. ast := NewAstWithToken(AstKindStatements, parser.current)
  429. var children []*Ast
  430. for parser.NextIsStatement() {
  431. child := parser.ParseStatement()
  432. children = append(children, child)
  433. }
  434. ast.AppendChildren(children...)
  435. return ast
  436. }
  437. func (parser *Parser) ParseProgram() *Ast {
  438. parser.LogDebug("ParseProgram: %s\n", parser.current.String())
  439. stats := parser.ParseStatements()
  440. eof := parser.Require(TokenKindEOF)
  441. aeof := NewAstWithToken(AstKindEnd, eof)
  442. if eof.IsNone() {
  443. aeof = parser.NewAstError("Expected EOF, have: %s", parser.current.String())
  444. }
  445. children := []*Ast{stats, aeof}
  446. return NewAst(AstKindProgram, nil, children, NoToken())
  447. }
  448. func (parser *Parser) Parse() (ast *Ast) {
  449. defer func() {
  450. if err := recover() ; err != nil {
  451. if perr, ok := err.(ParserError) ; ok{
  452. ast = parser.NewAstError("Parse error, recovered: %s\n", perr)
  453. } else {
  454. panic(err)
  455. }
  456. }
  457. } ()
  458. parser.Advance()
  459. ast = parser.ParseProgram()
  460. return ast
  461. }
  462. func NewParser(lexer *Lexer) *Parser {
  463. parser := &Parser{Lexer: lexer, current: NoToken(), LoggerWrapper: LoggerWrapper{nil}}
  464. parser.AddDefaultKeywords()
  465. return parser
  466. }
  467. func NewParserFromString(input string) *Parser {
  468. lexer := NewLexerFromString(input)
  469. return NewParser(lexer)
  470. }
  471. func NewParserFromFilename(filename string) (*Parser, error) {
  472. lexer, err := NewLexerFromFilename(filename)
  473. if err != nil {
  474. return nil, err
  475. }
  476. return NewParser(lexer), nil
  477. }
  478. func (parser * Parser) NewKeyword(name string, tk TokenKind, val Value) *Keyword {
  479. return parser.Lexer.NewKeyword(name, tk, val)
  480. }
  481. func (parser * Parser) AddKeyword(kw * Keyword) *Keyword {
  482. return parser.Lexer.AddKeyword(kw)
  483. }
  484. func (parser * Parser) AddKeywords(keywords []*Keyword) {
  485. for _, kw := range keywords {
  486. parser.AddKeyword(kw)
  487. }
  488. }
  489. var DefaultKeywords []*Keyword = []*Keyword{
  490. &Keyword { "true", TokenKindBoolean, TrueValue },
  491. &Keyword { "false", TokenKindBoolean, FalseValue },
  492. &Keyword { "nil", TokenKindNil, NilValue },
  493. &Keyword { "do", TokenKindOpenBlock, StringValue("{") },
  494. &Keyword { "end", TokenKindCloseBlock, StringValue("}") },
  495. &Keyword { "as", TokenKindOpenParen, StringValue("(") },
  496. &Keyword { "so", TokenKindCloseParen, StringValue(")") },
  497. &Keyword { "list", TokenKindOpenList, StringValue("[") },
  498. &Keyword { "done", TokenKindCloseList, StringValue("]") },
  499. &Keyword { "the", TokenKindGet, StringValue("$") },
  500. &Keyword { "a", TokenKindGet, StringValue("$") },
  501. &Keyword { "an", TokenKindGet, StringValue("$") },
  502. }
  503. var DefaultKeywordsFR []*Keyword = []*Keyword{
  504. &Keyword { "vrai", TokenKindBoolean, TrueValue },
  505. &Keyword { "faux", TokenKindBoolean, FalseValue },
  506. &Keyword { "nul", TokenKindNil, NilValue },
  507. &Keyword { "fais", TokenKindOpenBlock, StringValue("{") },
  508. &Keyword { "fin", TokenKindCloseBlock, StringValue("}") },
  509. &Keyword { "comme", TokenKindOpenParen, StringValue("(") },
  510. &Keyword { "ça", TokenKindCloseParen, StringValue(")") },
  511. &Keyword { "liste", TokenKindOpenList, StringValue("[") },
  512. &Keyword { "fini", TokenKindCloseList, StringValue("]") },
  513. &Keyword { "le", TokenKindGet, StringValue("$") },
  514. &Keyword { "la", TokenKindGet, StringValue("$") },
  515. }
  516. func (parser * Parser) AddDefaultKeywords() {
  517. parser.AddKeywords(DefaultKeywords)
  518. }