parser.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. In Muesli, the program is first tokenized by a lexer. The lexer uses the first
  19. character, and only the first character of every token to determine it's type.
  20. Then, the tokens are parsed with a recursive descent parser with a structured
  21. syntax, which has been verified to be LL(1) on smlweb.cpsc.ucalgary.ca. This
  22. means the parser only needs one token of lookahead to be able to parse the
  23. syntax. The syntax can be described as follows:
  24. PROGRAM -> STATEMENTS.
  25. STATEMENTS -> STATEMENT eos STATEMENTS|.
  26. STATEMENT -> EXPRESSION OPERATION |.
  27. OPERATION -> operator STATEMENT |.
  28. EXPRESSION -> COMMAND | EVALUATION.
  29. COMMAND -> FIELD PARAMETERS.
  30. FIELD -> NAME SELECTOR.
  31. SELECTOR -> selector PARAMETER |.
  32. PARAMETERS -> PARAMETER PARAMETERS |.
  33. PARAMETER -> FIELD | EVALUATION.
  34. EVALUATION -> LITERAL | BLOCK | GETTER | SETTER | LIST | PARENTHESIS.
  35. PARENTHESIS -> closeparen STATEMENT openparen.
  36. BLOCK -> openblock STATEMENTS closeblock.
  37. LIST -> openlist PARAMETERS closelist.
  38. LITERAL -> string | int | float.
  39. NAME -> word | symbol | type.
  40. SETTER -> set PARAMETER PARAMETER.
  41. GETTER -> get PARAMETER.
  42. The semantics of a muesli program are based on this syntax and can be explained
  43. as follows:
  44. - A muesli program consists of statements, separated by end of statement or eos,
  45. which are executed from top to bottom. The period . and the unescaped newline
  46. characters are eos.
  47. - A statement an expression followed by an optional operation, or
  48. the empty statement. A statement evaluates to the value of it's expression
  49. to which the consecutive operations are applied from right to left.
  50. - An expression is either a command or an evaluation.
  51. - An operation starts with an operator followed by a statement.
  52. That statement may in turn contain another operation.
  53. - An operator begins with a token with one of the following characters: -+/*^%~|&><@
  54. Next is a statement. The effect of an operator is to evaluate the
  55. links before it and after it as a substitution, and then look up the callable
  56. with the name of the operator and execute that.
  57. - A command consists of a field and parameters .
  58. - A field is a name optionally followed by selectors .
  59. - An evaluation is a block, parenthesis, list, getter, setter, or literal.
  60. - A parameter is an evaluation or a field.
  61. - A selector starts with a token with one of the following characters: ,;'
  62. Next is a parameter. The effect of a selector is to evaluate the
  63. parameter, and then use it as a string to look up the value of the field
  64. thus named in named object.
  65. - A block consist of statements between braces { }. The statements of a block
  66. are not executed but compiled to a block which can be called to execute it
  67. later.
  68. - A () parenthesis gets substituted inline by the value returned by executing it
  69. anywhere it occurs, also in the beginning of a command.
  70. - A bracketed list [ elem1 elem2 ... ] is syntactic sugar for (list elem1 elem 2)
  71. - A dollar getter $varname is syntactic sugar for (get varname)
  72. - A equals setter =varname value is syntactic sugar for (set varname value)
  73. - Therefore, parenthesis, lists, getters and setters are allowed anywhere,
  74. also in the beginning of the command with substitution semantics.
  75. - A literal evaluates to itself.
  76. - A name also evaluates as itself but is specific for commands.
  77. or, yet again:
  78. # Type a grammar here:
  79. PROGRAM -> STATEMENTS.
  80. STATEMENTS -> STATEMENT eos STATEMENTS|.
  81. STATEMENT -> EXPRESSION OPERATION |.
  82. OPERATION -> operator STATEMENT |.
  83. EXPRESSION -> COMMAND | EVALUATION .
  84. COMMAND -> NAME PARAMETERS.
  85. PARAMETERS -> PARAMETER PARAMETERS |.
  86. PARAMETER -> EVALUATION | NAME .
  87. EVALUATION -> LITERAL | BLOCK | GETTER | SETTER | LIST | PARENTHESIS | TAKE | MAKE | METHOD .
  88. PARENTHESIS -> closeparen STATEMENT openparen.
  89. BLOCK -> openblock STATEMENTS closeblock.
  90. LIST -> openlist PARAMETERS closelist.
  91. SETTER -> set PARAMETER PARAMETER.
  92. GETTER -> get PARAMETER.
  93. MAKE -> make FIELD PARAMETER .
  94. TAKE -> take FIELD .
  95. METHOD -> call FIELD .
  96. FIELD -> NAME SELECTORS.
  97. SELECTORS -> selector NAME SELECTORS |.
  98. LITERAL -> string | int | float.
  99. NAME -> word | symbol | type.
  100. */
  101. type Parser struct {
  102. Lexer *Lexer
  103. current Token
  104. LoggerWrapper
  105. Errors []ParserError
  106. }
  107. // panic with this type on errors that would prevent the parser
  108. // from making progress.
  109. type ParserError struct {
  110. *Parser
  111. *Token
  112. Chain error
  113. }
  114. func (pe ParserError) Error() string {
  115. return fmt.Sprintf("%s %s", pe.Token.String(), pe.Chain.Error())
  116. }
  117. func (parser *Parser) SetLogger(logger Logger) {
  118. parser.LoggerWrapper = LoggerWrapper{logger}
  119. }
  120. func (parser *Parser) SetDefaultLogger() {
  121. lg := &testLogger{}
  122. parser.LoggerWrapper = LoggerWrapper{lg}
  123. }
  124. func (parser *Parser) SetVmLogger(vm * VM) {
  125. parser.LoggerWrapper = vm.Console.LoggerWrapper
  126. }
  127. func (parser *Parser) Errorf(message string, args ...interface{}) ParserError {
  128. err := fmt.Errorf(message, args...)
  129. pe := ParserError { Parser: parser, Token:&parser.current, Chain: err }
  130. parser.Errors = append(parser.Errors, pe)
  131. return pe
  132. }
  133. func (parser *Parser) Panicf(message string, args ...interface{}) {
  134. pe := parser.Errorf(message, args...)
  135. panic(pe)
  136. }
  137. func (parser *Parser) Advance() {
  138. token := parser.Lexer.Lex()
  139. parser.current = token
  140. parser.LogDebug("Next token: %s\n", token.String())
  141. }
  142. /* Skips tokens until a closer, namely ), }, ], EOX or EOF is found,
  143. * to skip parse errors, and to be able to continue parsing despite errors. */
  144. func (parser *Parser) SkipError() Token {
  145. parser.Advance()
  146. for {
  147. parser.Advance()
  148. if parser.NextIsErrorSkipped() {
  149. return parser.current
  150. }
  151. if parser.NextIs(TokenKindError, TokenKindNone) {
  152. parser.Panicf("Cannot recover from parse error. Parse ended.")
  153. }
  154. }
  155. return parser.current
  156. }
  157. /* Looks at the current token and advances the lexer if the token is of any of
  158. the token kinds given in kinds. In this case it will return the accepted
  159. token and advance the parser. Otherwise, it will call parser.Panicf with a
  160. * "Syntax error unexpected <token>".*/
  161. func (parser *Parser) Require(kinds ...TokenKind) Token {
  162. if parser.current.IsNone() {
  163. parser.Advance()
  164. }
  165. expected := ""
  166. sep := ""
  167. for _, kind := range kinds {
  168. if kind == parser.current.TokenKind {
  169. accepted := parser.current
  170. parser.Advance()
  171. parser.LogDebug("Require: Accepted token: %s\n", accepted.String())
  172. return accepted
  173. }
  174. expected = fmt.Sprintf("%s%s%s", expected, sep, kind.String())
  175. }
  176. parser.Panicf("error: expected one of the following: %s", expected)
  177. return Token{}
  178. }
  179. func (parser *Parser) NewAstError(message string, args ...interface{}) *Ast {
  180. sv := StringValue(fmt.Sprintf(message+" at token "+parser.current.String(), args))
  181. pos := parser.current.Position
  182. tok := NewToken(TokenKindError, sv, pos)
  183. parser.Errorf(message, args...)
  184. return NewAst(AstKindError, nil, EmptyAstArray(), tok)
  185. }
  186. // Also handles none or error vales of the token
  187. func (parser *Parser) NewAst(kind AstKind, parent * Ast, children []*Ast, value Token) *Ast{
  188. if value.IsNone() {
  189. return NewAstNone()
  190. }
  191. if value.IsError() {
  192. return NewAst(AstKindError, parent, children, value)
  193. }
  194. return NewAst(kind, parent, children, value)
  195. }
  196. func (parser *Parser) ParseValue() *Ast {
  197. parser.LogDebug("ParseValue: %s\n", parser.current.String())
  198. value := parser.Require(TokenKindInteger, TokenKindString,
  199. TokenKindBoolean, TokenKindNil, TokenKindFloat, TokenKindSymbol)
  200. return parser.NewAst(AstKindValue, nil, EmptyAstArray(), value)
  201. }
  202. func AstKindForToken(token Token) AstKind {
  203. switch token.TokenKind {
  204. case TokenKindInteger, TokenKindString, TokenKindBoolean,
  205. TokenKindNil, TokenKindFloat, TokenKindSymbol:
  206. return AstKindValue
  207. case TokenKindWord:
  208. return AstKindWord
  209. case TokenKindType:
  210. return AstKindType
  211. default:
  212. return AstKindError
  213. }
  214. }
  215. func (parser *Parser) ParseLiteral() *Ast {
  216. parser.LogDebug("ParseLiteral: %s\n", parser.current.String())
  217. value := parser.Require(TokenKindInteger, TokenKindString,
  218. TokenKindBoolean, TokenKindNil, TokenKindFloat)
  219. astKind := AstKindForToken(value)
  220. return parser.NewAst(astKind, nil, EmptyAstArray(), value)
  221. }
  222. func (parser *Parser) ParseName() *Ast {
  223. parser.LogDebug("ParseName: %s\n", parser.current.String())
  224. value := parser.Require(TokenKindWord, TokenKindType, TokenKindSymbol)
  225. astKind := AstKindForToken(value)
  226. return parser.NewAst(astKind, nil, EmptyAstArray(), value)
  227. }
  228. func (parser *Parser) ParseArgument() *Ast {
  229. parser.LogDebug("ParseArgument: %s\n", parser.current.String())
  230. switch {
  231. case parser.NextIsName(): return parser.ParseField()
  232. case parser.NextIsBlock(): return parser.ParseBlock()
  233. case parser.NextIsSubstitution(): return parser.ParseSubstitution()
  234. case parser.NextIsLiteral(): return parser.ParseLiteral()
  235. default: parser.Panicf("error: in argument: expected $, =, }, word or value")
  236. return nil
  237. }
  238. }
  239. func (parser *Parser) ParseArguments(extra ... *Ast) *Ast {
  240. parser.LogDebug("ParseArguments: %s\n", parser.current.String())
  241. ast := NewAstWithToken(AstKindArguments, parser.current)
  242. var children []*Ast
  243. for _, arg := range extra {
  244. children = append(children, arg)
  245. }
  246. for parser.NextIsArgument() {
  247. child := parser.ParseArgument()
  248. children = append(children, child)
  249. }
  250. ast.AppendChildren(children...)
  251. return ast
  252. }
  253. func (parser *Parser) ParseList() *Ast {
  254. parser.LogDebug("ParseList: %s\n", parser.current.String())
  255. op := parser.Require(TokenKindOpenList)
  256. if op.IsError() {
  257. return parser.NewAstError("Unexpected value.")
  258. }
  259. ast := NewAstWithToken(AstKindList, op)
  260. args := parser.ParseArguments()
  261. if AstIsError(args) {
  262. return args
  263. }
  264. if cp := parser.Require(TokenKindCloseList); cp.IsError() {
  265. return parser.NewAstError("expected closing brackets")
  266. }
  267. ast.AppendChild(args)
  268. return ast
  269. }
  270. func (parser *Parser) ParseParenthesis() *Ast {
  271. parser.LogDebug("ParseParenthesis: %s\n", parser.current.String())
  272. op := parser.Require(TokenKindOpenParen)
  273. if op.IsError() {
  274. return parser.NewAstError("Unexpected value.")
  275. }
  276. ast := NewAstWithToken(AstKindParenthesis, op)
  277. expr := parser.ParseChain()
  278. if expr.IsNone() {
  279. return parser.NewAstError("expected chain")
  280. }
  281. if AstIsError(expr) {
  282. return expr
  283. }
  284. if cp := parser.Require(TokenKindCloseParen); cp.IsError() {
  285. return parser.NewAstError("expected closing parenthesis")
  286. }
  287. ast.AppendChild(expr)
  288. return ast
  289. }
  290. func (parser *Parser) ParseBlock() *Ast {
  291. parser.LogDebug("ParseBlock: %s\n", parser.current.String())
  292. op := parser.Require(TokenKindOpenBlock)
  293. if op.IsError() {
  294. return parser.NewAstError("Unexpected value.")
  295. }
  296. ast := NewAstWithToken(AstKindBlock, op)
  297. stats := parser.ParseStatements()
  298. if stats.IsNone() {
  299. return parser.NewAstError("expected expression")
  300. }
  301. if AstIsError(stats) {
  302. return stats
  303. }
  304. if cp := parser.Require(TokenKindCloseBlock); cp.IsError() {
  305. return parser.NewAstError("expected closing block")
  306. }
  307. ast.AppendChild(stats)
  308. return ast
  309. }
  310. func (parser *Parser) RequireName() Token {
  311. return parser.Require(TokenKindWord, TokenKindType, TokenKindSymbol)
  312. }
  313. /* Parses the target of a set expression */
  314. func (parser *Parser) ParseTarget() *Ast {
  315. parser.LogDebug("ParseTarget: %s\n", parser.current.String())
  316. var target *Ast
  317. switch {
  318. case parser.NextIsName():
  319. // Direct target to a set
  320. token := parser.RequireName()
  321. target = NewAstWithToken(AstKindTarget, token)
  322. case parser.NextIsSubstitution():
  323. // Indirect target
  324. target = NewAstWithToken(AstKindTarget, parser.current)
  325. target.AppendChild(parser.ParseSubstitution())
  326. default:
  327. parser.Panicf("Malformed setter.")
  328. return nil
  329. }
  330. argument := parser.ParseArgument()
  331. if argument.IsNone() {
  332. return parser.NewAstError("Expected argument to set")
  333. }
  334. target.AppendChild(argument)
  335. return target
  336. }
  337. /* Parses the origin of a get expression */
  338. func (parser *Parser) ParseOrigin() *Ast {
  339. parser.LogDebug("ParseOrigin: %s\n", parser.current.String())
  340. var target *Ast
  341. switch {
  342. case parser.NextIsName():
  343. // Direct target to a set
  344. token := parser.RequireName()
  345. target = NewAstWithToken(AstKindTarget, token)
  346. case parser.NextIsSubstitution():
  347. target = NewAstWithToken(AstKindTarget, parser.current)
  348. target.AppendChild(parser.ParseSubstitution())
  349. default:
  350. parser.Panicf("Malformed getter")
  351. return nil
  352. }
  353. return target
  354. }
  355. func (parser *Parser) ParseSelector() *Ast {
  356. parser.LogDebug("ParseSelector %s\n", parser.current.String())
  357. selector := parser.Require(TokenKindSelector)
  358. parameter := parser.ParseArgument()
  359. ast := NewAstWithToken(AstKindSelector, selector)
  360. ast.AppendChild(parameter)
  361. return ast
  362. }
  363. // Parses a Field with it's optional selectors
  364. func (parser *Parser) ParseField() *Ast {
  365. // FIELD -> NAME SELECTOR.
  366. name := parser.ParseName()
  367. if parser.NextIsSelector() {
  368. selector := parser.ParseSelector()
  369. // prepend name before selector
  370. selector.children = append([]*Ast{name}, selector.children...)
  371. }
  372. // No selector, just return the name
  373. return name
  374. }
  375. func (parser *Parser) ParseSet() *Ast {
  376. parser.LogDebug("ParseSet: %s\n", parser.current.String())
  377. set := parser.Require(TokenKindSet)
  378. ast := NewAstWithToken(AstKindSet, set)
  379. target := parser.ParseTarget()
  380. ast.AppendChild(target)
  381. return ast
  382. }
  383. func (parser *Parser) ParseGet() *Ast {
  384. parser.LogDebug("ParseGet: %s\n", parser.current.String())
  385. get := parser.Require(TokenKindGet)
  386. ast := NewAstWithToken(AstKindGet, get)
  387. target := parser.ParseOrigin()
  388. ast.AppendChild(target)
  389. return ast
  390. }
  391. func (parser *Parser) ParseCommand() *Ast {
  392. parser.LogDebug("ParseCommand: %s\n", parser.current.String())
  393. field := parser.ParseField()
  394. arguments := parser.ParseArguments()
  395. /// this is wrong...
  396. command := NewAstWithToken(AstKindCommand, field.Token())
  397. command.AppendChild(arguments)
  398. return command
  399. }
  400. func (parser *Parser) ParseSubstitution() *Ast {
  401. parser.LogDebug("ParseSubstitution: %s\n", parser.current.String())
  402. switch {
  403. case parser.NextIs(TokenKindOpenParen): return parser.ParseParenthesis()
  404. case parser.NextIs(TokenKindOpenList): return parser.ParseList()
  405. case parser.NextIs(TokenKindGet): return parser.ParseGet()
  406. case parser.NextIs(TokenKindSet): return parser.ParseSet()
  407. default:
  408. parser.Panicf("Syntax error in substitution, expected $, =, (, [")
  409. return nil
  410. }
  411. }
  412. func (parser *Parser) ParseExpression() *Ast {
  413. parser.LogDebug("ParseExpression: %s\n", parser.current.String())
  414. switch {
  415. case parser.NextIsWord(): return parser.ParseCommand()
  416. case parser.NextIsSubstitution(): return parser.ParseSubstitution()
  417. case parser.NextIsLiteral(): return parser.ParseLiteral()
  418. default:
  419. parser.Panicf("Syntax error in expression, expected word, $, =, value")
  420. return nil
  421. }
  422. }
  423. func (parser *Parser) ParseExpressionStatement() *Ast {
  424. parser.LogDebug("ParseExpressionStatement: %s\n", parser.current.String())
  425. expr := parser.ParseExpression()
  426. if expr.IsNone() {
  427. return NewAstNone()
  428. }
  429. // Expression statements must end on EOX or EOF
  430. if eox := parser.Require(TokenKindEOX, TokenKindEOF); eox.IsError() {
  431. return parser.NewAstError("expected end of statement")
  432. }
  433. return expr
  434. }
  435. func (parser *Parser) ParseEmptyStatement() *Ast {
  436. parser.LogDebug("ParseEmptyStatement: %s\n", parser.current.String())
  437. if eox := parser.Require(TokenKindEOX, TokenKindEOF); eox.IsError() {
  438. return parser.NewAstError("expected end of statement")
  439. }
  440. return NewAstWithToken(AstKindStatement, parser.current)
  441. }
  442. func (parser Parser) NextIs(kinds ...TokenKind) bool {
  443. if (parser.current.TokenKind == TokenKindNone) {
  444. parser.Advance()
  445. }
  446. for _, kind := range kinds {
  447. if kind == parser.current.TokenKind {
  448. return true
  449. }
  450. }
  451. return false
  452. }
  453. func (parser Parser) NextIsBlock() bool {
  454. return parser.NextIs(TokenKindOpenBlock)
  455. }
  456. func (parser Parser) NextIsSubstitution() bool {
  457. return parser.NextIs(TokenKindOpenList, TokenKindOpenParen,
  458. TokenKindSet, TokenKindGet)
  459. }
  460. func (parser Parser) NextIsWord() bool {
  461. return parser.NextIs(TokenKindWord)
  462. }
  463. func (parser Parser) NextIsGet() bool {
  464. return parser.NextIs(TokenKindGet)
  465. }
  466. func (parser Parser) NextIsSet() bool {
  467. return parser.NextIs(TokenKindSet)
  468. }
  469. func (parser Parser) NextIsLiteral() bool {
  470. return parser.NextIs(TokenKindString, TokenKindInteger, TokenKindFloat,
  471. TokenKindBoolean, TokenKindNil)
  472. }
  473. func (parser Parser) NextIsName() bool {
  474. return parser.NextIs(TokenKindWord, TokenKindType, TokenKindSymbol)
  475. }
  476. func (parser Parser) NextIsOperator() bool {
  477. return parser.NextIs(TokenKindOperator)
  478. }
  479. func (parser Parser) NextIsSelector() bool {
  480. return parser.NextIs(TokenKindSelector)
  481. }
  482. func (parser Parser) NextIsArgument() bool {
  483. return parser.NextIsName() || parser.NextIsLiteral() ||
  484. parser.NextIsSubstitution() || parser.NextIsBlock()
  485. }
  486. func (parser Parser) NextIsCommand() bool {
  487. return parser.NextIsName()
  488. }
  489. func (parser Parser) NextIsErrorSkipped() bool {
  490. return parser.NextIs(TokenKindCloseBlock, TokenKindCloseList, TokenKindCloseParen,
  491. TokenKindEOX, TokenKindEOF)
  492. }
  493. func (parser Parser) NextIsEOX() bool {
  494. return parser.NextIs(TokenKindEOX)
  495. }
  496. func (parser Parser) NextIsEOF() bool {
  497. return parser.NextIs(TokenKindEOF)
  498. }
  499. func (parser Parser) NextIsExpression() bool {
  500. return parser.NextIsCommand() || parser.NextIsSubstitution() ||
  501. parser.NextIsLiteral()
  502. }
  503. func (parser Parser) NextIsChain() bool {
  504. return parser.NextIsExpression()
  505. }
  506. func (parser Parser) NextIsStatement() bool {
  507. return parser.NextIsChain() || parser.NextIsBlock() || parser.NextIsEOX()
  508. }
  509. func (parser *Parser) newMethodChain(oper Token, expr1, expr2 *Ast) *Ast {
  510. chain := NewAstWithToken(AstKindOperation, oper)
  511. chain.AppendChildren(expr1)
  512. chain.AppendChild(NewAstWithToken(AstKindForToken(expr2.Token()), expr2.Token()))
  513. chain.AppendChildren(expr2.Children()...)
  514. parser.LogDebug("New method chain: %v", chain)
  515. return chain
  516. }
  517. func (parser *Parser) newChain(oper Token, expr1, expr2 *Ast) * Ast {
  518. var astkind AstKind = AstKindParenthesis
  519. subst1 := NewAstWithToken(astkind, oper)
  520. subst1.AppendChildren(expr1)
  521. subst2 := NewAstWithToken(astkind, oper)
  522. subst2.AppendChildren(expr2)
  523. chain := NewAstWithToken(AstKindOperation, oper)
  524. chain.AppendChildren(subst1, subst2)
  525. return chain
  526. }
  527. func (parser *Parser) composeChain(oper Token, chain, nextExpr *Ast) * Ast {
  528. var astkind AstKind = AstKindParenthesis
  529. parser.LogDebug("Composed chain: %v", chain)
  530. subst := NewAstWithToken(astkind, oper)
  531. subst.AppendChildren(nextExpr)
  532. newChain := NewAstWithToken(AstKindOperation, oper)
  533. newChain.AppendChildren(chain, subst)
  534. return newChain
  535. }
  536. func (parser *Parser) ParseChain() *Ast {
  537. expression := parser.ParseExpression()
  538. if !parser.NextIsOperator() {
  539. return expression
  540. }
  541. var expressions = []*Ast{ expression }
  542. var operators = []Token {}
  543. for parser.NextIsOperator() {
  544. oper := parser.Require(TokenKindOperator)
  545. expression := parser.ParseExpression()
  546. if expression == nil {
  547. parser.Panicf("Expected expression after operator.")
  548. }
  549. expressions = append(expressions, expression)
  550. operators = append(operators, oper)
  551. }
  552. // Now there are N expressions and N - 1 operators, iterate
  553. // for easy composition
  554. var chain *Ast
  555. parser.LogDebug("Working on operator chain: %v %v", operators, expressions)
  556. for i, j := 0, 0 ; i < len(expressions) && j < len(operators) ; i, j = i + 1 , j + 1 {
  557. expression := expressions[i]
  558. oper := operators[j]
  559. if chain == nil {
  560. expression2 := expressions[i+1]
  561. chain = parser.newChain(oper, expression, expression2)
  562. i++
  563. } else {
  564. chain = parser.composeChain(oper, chain, expression)
  565. }
  566. }
  567. return chain
  568. }
  569. func (parser *Parser) ParseStatement() *Ast {
  570. parser.LogDebug("ParseStatement: %s\n", parser.current.String())
  571. switch {
  572. case parser.NextIsBlock(): return parser.ParseBlock()
  573. case parser.NextIsExpression(): return parser.ParseChain()
  574. case parser.NextIsEOX(): return parser.ParseEmptyStatement()
  575. default:
  576. parser.Panicf("Expected block, command, or EOX in statement")
  577. return parser.NewAstError("Expected closed, expression, or EOX in statement")
  578. }
  579. }
  580. func (parser *Parser) ParseStatements() *Ast {
  581. parser.LogDebug("ParseStatements: %s\n", parser.current.String())
  582. ast := NewAstWithToken(AstKindStatements, parser.current)
  583. var children []*Ast
  584. for parser.NextIsStatement() {
  585. child := parser.ParseStatement()
  586. children = append(children, child)
  587. }
  588. ast.AppendChildren(children...)
  589. return ast
  590. }
  591. func (parser *Parser) ParseProgram() *Ast {
  592. parser.LogDebug("ParseProgram: %s\n", parser.current.String())
  593. stats := parser.ParseStatements()
  594. eof := parser.Require(TokenKindEOF)
  595. aeof := NewAstWithToken(AstKindEnd, eof)
  596. if eof.IsNone() {
  597. aeof = parser.NewAstError("Expected EOF, have: %s", parser.current.String())
  598. }
  599. children := []*Ast{stats, aeof}
  600. return NewAst(AstKindProgram, nil, children, NoToken())
  601. }
  602. func (parser *Parser) Parse() (ast *Ast) {
  603. defer func() {
  604. if err := recover() ; err != nil {
  605. if perr, ok := err.(ParserError) ; ok{
  606. ast = parser.NewAstError("Parse error, recovered: %s\n", perr)
  607. } else {
  608. panic(err)
  609. }
  610. }
  611. } ()
  612. parser.Advance()
  613. ast = parser.ParseProgram()
  614. return ast
  615. }
  616. func NewParser(lexer *Lexer) *Parser {
  617. parser := &Parser{Lexer: lexer, current: NoToken(), LoggerWrapper: LoggerWrapper{nil}}
  618. return parser
  619. }
  620. func NewParserFromString(input string) *Parser {
  621. lexer := NewLexerFromString(input)
  622. return NewParser(lexer)
  623. }
  624. func NewParserFromFilename(filename string) (*Parser, error) {
  625. lexer, err := NewLexerFromFilename(filename)
  626. if err != nil {
  627. return nil, err
  628. }
  629. return NewParser(lexer), nil
  630. }
  631. func (parser * Parser) NewKeyword(name string, tk TokenKind, val Value) *Keyword {
  632. return parser.Lexer.NewKeyword(name, tk, val)
  633. }
  634. func (parser * Parser) AddKeyword(kw * Keyword) *Keyword {
  635. return parser.Lexer.AddKeyword(kw)
  636. }
  637. func (parser * Parser) AddKeywords(keywords []*Keyword) {
  638. for _, kw := range keywords {
  639. parser.AddKeyword(kw)
  640. }
  641. }
  642. func (parser * Parser) AddDefaultKeywords() {
  643. parser.AddKeywords(DefaultKeywords)
  644. }