parser.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package parser
  11. import (
  12. "bytes"
  13. "fmt"
  14. "devt.de/krotik/common/stringutil"
  15. )
  16. // AST Nodes
  17. // =========
  18. /*
  19. ASTNode models a node in the AST
  20. */
  21. type ASTNode struct {
  22. Name string // Name of the node
  23. Token *LexToken // Lexer token of this ASTNode
  24. Children []*ASTNode // Child nodes
  25. Runtime Runtime // Runtime component for this ASTNode
  26. binding int // Binding power of this node
  27. nullDenotation func(p *parser, self *ASTNode) (*ASTNode, error) // Configure token as beginning node
  28. leftDenotation func(p *parser, self *ASTNode, left *ASTNode) (*ASTNode, error) // Configure token as left node
  29. }
  30. /*
  31. ASTFromPlain creates an AST from a plain AST.
  32. A plain AST is a nested map structure like this:
  33. {
  34. name : <name of node>
  35. value : <value of node>
  36. children : [ <child nodes> ]
  37. }
  38. */
  39. func ASTFromPlain(plainAST map[string]interface{}) (*ASTNode, error) {
  40. var astChildren []*ASTNode
  41. name, ok := plainAST["name"]
  42. if !ok {
  43. return nil, fmt.Errorf("Found plain ast node without a name: %v", plainAST)
  44. }
  45. value, ok := plainAST["value"]
  46. if !ok {
  47. return nil, fmt.Errorf("Found plain ast node without a value: %v", plainAST)
  48. }
  49. // Create children
  50. if children, ok := plainAST["children"]; ok {
  51. if ic, ok := children.([]interface{}); ok {
  52. // Do a list conversion if necessary - this is necessary when we parse
  53. // JSON with map[string]interface{} this
  54. childrenList := make([]map[string]interface{}, len(ic))
  55. for i := range ic {
  56. childrenList[i] = ic[i].(map[string]interface{})
  57. }
  58. children = childrenList
  59. }
  60. for _, child := range children.([]map[string]interface{}) {
  61. astChild, err := ASTFromPlain(child)
  62. if err != nil {
  63. return nil, err
  64. }
  65. astChildren = append(astChildren, astChild)
  66. }
  67. }
  68. return &ASTNode{fmt.Sprint(name), &LexToken{TokenGeneral, 0,
  69. fmt.Sprint(value), 0, 0}, astChildren, nil, 0, nil, nil}, nil
  70. }
  71. /*
  72. Create a new instance of this ASTNode which is connected to a concrete lexer token.
  73. */
  74. func (n *ASTNode) instance(p *parser, t *LexToken) *ASTNode {
  75. ret := &ASTNode{n.Name, t, make([]*ASTNode, 0, 2), nil, n.binding, n.nullDenotation, n.leftDenotation}
  76. if p.rp != nil {
  77. ret.Runtime = p.rp.Runtime(ret)
  78. }
  79. return ret
  80. }
  81. /*
  82. Plain returns this ASTNode and all its children as plain AST. A plain AST
  83. only contains map objects, lists and primitive types which can be serialized
  84. with JSON.
  85. */
  86. func (n *ASTNode) Plain() map[string]interface{} {
  87. ret := make(map[string]interface{})
  88. ret["name"] = n.Name
  89. lenChildren := len(n.Children)
  90. if lenChildren > 0 {
  91. children := make([]map[string]interface{}, lenChildren)
  92. for i, child := range n.Children {
  93. children[i] = child.Plain()
  94. }
  95. ret["children"] = children
  96. }
  97. // The value is what the lexer found in the source
  98. ret["value"] = n.Token.Val
  99. return ret
  100. }
  101. /*
  102. String returns a string representation of this token.
  103. */
  104. func (n *ASTNode) String() string {
  105. var buf bytes.Buffer
  106. n.levelString(0, &buf)
  107. return buf.String()
  108. }
  109. /*
  110. levelString function to recursively print the tree.
  111. */
  112. func (n *ASTNode) levelString(indent int, buf *bytes.Buffer) {
  113. // Print current level
  114. buf.WriteString(stringutil.GenerateRollingString(" ", indent*2))
  115. if n.Name == NodeVALUE || (n.Name == NodeSHOWTERM && n.Token.Val != "@") {
  116. buf.WriteString(fmt.Sprintf(n.Name+": %v", n.Token))
  117. } else {
  118. buf.WriteString(n.Name)
  119. }
  120. buf.WriteString("\n")
  121. // Print children
  122. for _, child := range n.Children {
  123. child.levelString(indent+1, buf)
  124. }
  125. }
  126. /*
  127. Map of AST nodes corresponding to lexer tokens
  128. */
  129. var astNodeMap map[LexTokenID]*ASTNode
  130. /*
  131. TokenSHOWTERM is an extra token which is generated by the parser
  132. to group show terms
  133. */
  134. const TokenSHOWTERM = LexTokenID(-1)
  135. func init() {
  136. astNodeMap = map[LexTokenID]*ASTNode{
  137. TokenEOF: {NodeEOF, nil, nil, nil, 0, ndTerm, nil},
  138. TokenVALUE: {NodeVALUE, nil, nil, nil, 0, ndTerm, nil},
  139. TokenNODEKIND: {NodeVALUE, nil, nil, nil, 0, ndTerm, nil},
  140. TokenTRUE: {NodeTRUE, nil, nil, nil, 0, ndTerm, nil},
  141. TokenFALSE: {NodeFALSE, nil, nil, nil, 0, ndTerm, nil},
  142. TokenNULL: {NodeNULL, nil, nil, nil, 0, ndTerm, nil},
  143. TokenAT: {NodeFUNC, nil, nil, nil, 0, ndFunc, nil},
  144. TokenORDERING: {NodeORDERING, nil, nil, nil, 0, ndWithFunc, nil},
  145. TokenFILTERING: {NodeFILTERING, nil, nil, nil, 0, ndWithFunc, nil},
  146. TokenNULLTRAVERSAL: {NodeNULLTRAVERSAL, nil, nil, nil, 0, ndWithFunc, nil},
  147. // Special tokens - always handled in a denotation function
  148. TokenCOMMA: {NodeCOMMA, nil, nil, nil, 0, nil, nil},
  149. TokenGROUP: {NodeGROUP, nil, nil, nil, 0, nil, nil},
  150. TokenEND: {NodeEND, nil, nil, nil, 0, nil, nil},
  151. TokenAS: {NodeAS, nil, nil, nil, 0, nil, nil},
  152. TokenFORMAT: {NodeFORMAT, nil, nil, nil, 0, nil, nil},
  153. // Keywords
  154. TokenGET: {NodeGET, nil, nil, nil, 0, ndGet, nil},
  155. TokenLOOKUP: {NodeLOOKUP, nil, nil, nil, 0, ndLookup, nil},
  156. TokenFROM: {NodeFROM, nil, nil, nil, 0, ndFrom, nil},
  157. TokenWHERE: {NodeWHERE, nil, nil, nil, 0, ndPrefix, nil},
  158. TokenUNIQUE: {NodeUNIQUE, nil, nil, nil, 0, ndPrefix, nil},
  159. TokenUNIQUECOUNT: {NodeUNIQUECOUNT, nil, nil, nil, 0, ndPrefix, nil},
  160. TokenISNOTNULL: {NodeISNOTNULL, nil, nil, nil, 0, ndPrefix, nil},
  161. TokenASCENDING: {NodeASCENDING, nil, nil, nil, 0, ndPrefix, nil},
  162. TokenDESCENDING: {NodeDESCENDING, nil, nil, nil, 0, ndPrefix, nil},
  163. TokenTRAVERSE: {NodeTRAVERSE, nil, nil, nil, 0, ndTraverse, nil},
  164. TokenPRIMARY: {NodePRIMARY, nil, nil, nil, 0, ndPrefix, nil},
  165. TokenSHOW: {NodeSHOW, nil, nil, nil, 0, ndShow, nil},
  166. TokenSHOWTERM: {NodeSHOWTERM, nil, nil, nil, 0, ndShow, nil},
  167. TokenWITH: {NodeWITH, nil, nil, nil, 0, ndWith, nil},
  168. TokenLIST: {NodeLIST, nil, nil, nil, 0, nil, nil},
  169. // Boolean operations
  170. TokenNOT: {NodeNOT, nil, nil, nil, 20, ndPrefix, nil},
  171. TokenOR: {NodeOR, nil, nil, nil, 30, nil, ldInfix},
  172. TokenAND: {NodeAND, nil, nil, nil, 40, nil, ldInfix},
  173. TokenGEQ: {NodeGEQ, nil, nil, nil, 60, nil, ldInfix},
  174. TokenLEQ: {NodeLEQ, nil, nil, nil, 60, nil, ldInfix},
  175. TokenNEQ: {NodeNEQ, nil, nil, nil, 60, nil, ldInfix},
  176. TokenEQ: {NodeEQ, nil, nil, nil, 60, nil, ldInfix},
  177. TokenGT: {NodeGT, nil, nil, nil, 60, nil, ldInfix},
  178. TokenLT: {NodeLT, nil, nil, nil, 60, nil, ldInfix},
  179. TokenLIKE: {NodeLIKE, nil, nil, nil, 60, nil, ldInfix},
  180. TokenIN: {NodeIN, nil, nil, nil, 60, nil, ldInfix},
  181. TokenCONTAINS: {NodeCONTAINS, nil, nil, nil, 60, nil, ldInfix},
  182. TokenBEGINSWITH: {NodeBEGINSWITH, nil, nil, nil, 60, nil, ldInfix},
  183. TokenENDSWITH: {NodeENDSWITH, nil, nil, nil, 60, nil, ldInfix},
  184. TokenCONTAINSNOT: {NodeCONTAINSNOT, nil, nil, nil, 60, nil, ldInfix},
  185. TokenNOTIN: {NodeNOTIN, nil, nil, nil, 60, nil, ldInfix},
  186. // Simple arithmetic expressions
  187. TokenPLUS: {NodePLUS, nil, nil, nil, 110, ndPrefix, ldInfix},
  188. TokenMINUS: {NodeMINUS, nil, nil, nil, 110, ndPrefix, ldInfix},
  189. TokenTIMES: {NodeTIMES, nil, nil, nil, 120, nil, ldInfix},
  190. TokenDIV: {NodeDIV, nil, nil, nil, 120, nil, ldInfix},
  191. TokenMODINT: {NodeMODINT, nil, nil, nil, 120, nil, ldInfix},
  192. TokenDIVINT: {NodeDIVINT, nil, nil, nil, 120, nil, ldInfix},
  193. // Brackets
  194. TokenLPAREN: {NodeLPAREN, nil, nil, nil, 150, ndInner, nil},
  195. TokenRPAREN: {NodeRPAREN, nil, nil, nil, 0, nil, nil},
  196. TokenLBRACK: {NodeLBRACK, nil, nil, nil, 150, ndList, nil},
  197. TokenRBRACK: {NodeRBRACK, nil, nil, nil, 0, nil, nil},
  198. }
  199. }
  200. // Parser
  201. // ======
  202. /*
  203. Parser data structure
  204. */
  205. type parser struct {
  206. name string // Name to identify the input
  207. node *ASTNode // Current ast node
  208. tokens chan LexToken // Channel which contains lex tokens
  209. rp RuntimeProvider // Runtime provider which creates runtime components
  210. }
  211. /*
  212. Parse parses a given input string and returns an AST.
  213. */
  214. func Parse(name string, input string) (*ASTNode, error) {
  215. return ParseWithRuntime(name, input, nil)
  216. }
  217. /*
  218. ParseWithRuntime parses a given input string and returns an AST decorated with
  219. runtime components.
  220. */
  221. func ParseWithRuntime(name string, input string, rp RuntimeProvider) (*ASTNode, error) {
  222. p := &parser{name, nil, Lex(name, input), rp}
  223. node, err := p.next()
  224. if err != nil {
  225. return nil, err
  226. }
  227. p.node = node
  228. return p.run(0)
  229. }
  230. /*
  231. run models the main parser function.
  232. */
  233. func (p *parser) run(rightBinding int) (*ASTNode, error) {
  234. var err error
  235. n := p.node
  236. p.node, err = p.next()
  237. if err != nil {
  238. return nil, err
  239. }
  240. // Start with the null denotation of this statement / expression
  241. if n.nullDenotation == nil {
  242. return nil, p.newParserError(ErrImpossibleNullDenotation,
  243. n.Token.String(), *n.Token)
  244. }
  245. left, err := n.nullDenotation(p, n)
  246. if err != nil {
  247. return nil, err
  248. }
  249. // Collect left denotations as long as the left binding power is greater
  250. // than the initial right one
  251. for rightBinding < p.node.binding {
  252. var nleft *ASTNode
  253. n = p.node
  254. p.node, err = p.next()
  255. if err != nil {
  256. return nil, err
  257. }
  258. if n.leftDenotation == nil {
  259. return nil, p.newParserError(ErrImpossibleLeftDenotation,
  260. n.Token.String(), *n.Token)
  261. }
  262. // Get the next left denotation
  263. nleft, err = n.leftDenotation(p, n, left)
  264. left = nleft
  265. if err != nil {
  266. return nil, err
  267. }
  268. }
  269. return left, nil
  270. }
  271. /*
  272. next retrieves the next lexer token.
  273. */
  274. func (p *parser) next() (*ASTNode, error) {
  275. token, more := <-p.tokens
  276. if !more {
  277. // Unexpected end of input - the associated token is an empty error token
  278. return nil, p.newParserError(ErrUnexpectedEnd, "", token)
  279. } else if token.ID == TokenError {
  280. // There was a lexer error wrap it in a parser error
  281. return nil, p.newParserError(ErrLexicalError, token.Val, token)
  282. } else if node, ok := astNodeMap[token.ID]; ok {
  283. return node.instance(p, &token), nil
  284. }
  285. return nil, p.newParserError(ErrUnknownToken, fmt.Sprintf("id:%v (%v)", token.ID, token), token)
  286. }
  287. // Standard null denotation functions
  288. // ==================================
  289. /*
  290. ndTerm is used for terminals.
  291. */
  292. func ndTerm(p *parser, self *ASTNode) (*ASTNode, error) {
  293. return self, nil
  294. }
  295. /*
  296. ndInner returns the inner expression of an enclosed block and discard the
  297. block token. This method is used for brackets.
  298. */
  299. func ndInner(p *parser, self *ASTNode) (*ASTNode, error) {
  300. // Get the inner expression
  301. exp, err := p.run(0)
  302. if err != nil {
  303. return nil, err
  304. }
  305. // We return here the inner expression - discarding the bracket tokens
  306. return exp, skipToken(p, TokenRPAREN)
  307. }
  308. /*
  309. ndPrefix is used for prefix operators.
  310. */
  311. func ndPrefix(p *parser, self *ASTNode) (*ASTNode, error) {
  312. // Make sure a prefix will only prefix the next item
  313. val, err := p.run(self.binding + 20)
  314. if err != nil {
  315. return nil, err
  316. }
  317. self.Children = append(self.Children, val)
  318. return self, nil
  319. }
  320. // Null denotation functions for specific expressions
  321. // ==================================================
  322. /*
  323. ndGet is used to parse lookup expressions.
  324. */
  325. func ndGet(p *parser, self *ASTNode) (*ASTNode, error) {
  326. // Must specify a node kind
  327. if err := acceptChild(p, self, TokenNODEKIND); err != nil {
  328. return nil, err
  329. }
  330. // Parse the rest and add it as children
  331. for p.node.Token.ID != TokenEOF {
  332. exp, err := p.run(0)
  333. if err != nil {
  334. return nil, err
  335. }
  336. self.Children = append(self.Children, exp)
  337. }
  338. return self, nil
  339. }
  340. /*
  341. ndLookup is used to parse lookup expressions.
  342. */
  343. func ndLookup(p *parser, self *ASTNode) (*ASTNode, error) {
  344. // Must specify a node kind
  345. if err := acceptChild(p, self, TokenNODEKIND); err != nil {
  346. return nil, err
  347. }
  348. // Must have at least on node key
  349. if err := acceptChild(p, self, TokenVALUE); err != nil {
  350. return nil, err
  351. }
  352. // Read all commas and accept further values as additional node keys
  353. for skipToken(p, TokenCOMMA) == nil {
  354. if err := acceptChild(p, self, TokenVALUE); err != nil {
  355. return nil, err
  356. }
  357. }
  358. // Parse the rest and add it as children
  359. for p.node.Token.ID != TokenEOF {
  360. exp, err := p.run(0)
  361. if err != nil {
  362. return nil, err
  363. }
  364. self.Children = append(self.Children, exp)
  365. }
  366. return self, nil
  367. }
  368. /*
  369. ndFrom is used to parse from group ... expressions.
  370. */
  371. func ndFrom(p *parser, self *ASTNode) (*ASTNode, error) {
  372. // Must be followed by a group keyword
  373. if err := acceptChild(p, self, TokenGROUP); err != nil {
  374. return nil, err
  375. }
  376. // Must have a group name
  377. return self, acceptChild(p, self.Children[0], TokenVALUE)
  378. }
  379. /*
  380. ndTraverse is used to parse traverse expressions.
  381. */
  382. func ndTraverse(p *parser, self *ASTNode) (*ASTNode, error) {
  383. // Must be followed by traversal spec
  384. if err := acceptChild(p, self, TokenVALUE); err != nil {
  385. return nil, err
  386. }
  387. // Parse the rest and add it as children - must end with "end" if
  388. // further clauses are given
  389. for p.node.Token.ID != TokenEOF && p.node.Token.ID != TokenEND {
  390. exp, err := p.run(0)
  391. if err != nil {
  392. return nil, err
  393. }
  394. self.Children = append(self.Children, exp)
  395. }
  396. if p.node.Token.ID == TokenEND {
  397. skipToken(p, TokenEND)
  398. }
  399. return self, nil
  400. }
  401. /*
  402. ndFunc is used to parse functions.
  403. */
  404. func ndFunc(p *parser, self *ASTNode) (*ASTNode, error) {
  405. // Must specify a name
  406. if err := acceptChild(p, self, TokenVALUE); err != nil {
  407. return nil, err
  408. }
  409. // Must have an opening bracket
  410. if err := skipToken(p, TokenLPAREN); err != nil {
  411. return nil, err
  412. }
  413. // Read in the first attribute
  414. if p.node.Token.ID == TokenVALUE {
  415. // Next call cannot fail since we just checked for it. Value is optional.
  416. acceptChild(p, self, TokenVALUE)
  417. // Read all commas and accept further values as parameters until the end
  418. for skipToken(p, TokenCOMMA) == nil {
  419. if err := acceptChild(p, self, TokenVALUE); err != nil {
  420. return nil, err
  421. }
  422. }
  423. }
  424. // Must have a closing bracket
  425. return self, skipToken(p, TokenRPAREN)
  426. }
  427. /*
  428. ndShow is used to parse a show clauses.
  429. */
  430. func ndShow(p *parser, self *ASTNode) (*ASTNode, error) {
  431. acceptShowTerm := func() error {
  432. st := astNodeMap[TokenSHOWTERM].instance(p, p.node.Token)
  433. if p.node.Token.ID == TokenAT {
  434. // Parse a function
  435. exp, err := p.run(0)
  436. if err != nil {
  437. return err
  438. }
  439. st.Children = append(st.Children, exp)
  440. } else {
  441. // Skip the value token from which we just created an AST node
  442. skipToken(p, TokenVALUE)
  443. }
  444. // Parse an "as" definition if given
  445. if p.node.Token.ID == TokenAS {
  446. current := p.node
  447. acceptChild(p, st, TokenAS)
  448. if err := acceptChild(p, current, TokenVALUE); err != nil {
  449. return err
  450. }
  451. }
  452. // Parse a "format" definition if given
  453. if p.node.Token.ID == TokenFORMAT {
  454. current := p.node
  455. acceptChild(p, st, TokenFORMAT)
  456. if err := acceptChild(p, current, TokenVALUE); err != nil {
  457. return err
  458. }
  459. }
  460. self.Children = append(self.Children, st)
  461. return nil
  462. }
  463. // Read in the first node attribute
  464. if p.node.Token.ID == TokenVALUE || p.node.Token.ID == TokenAT {
  465. if err := acceptShowTerm(); err != nil {
  466. return nil, err
  467. }
  468. // Read further show entries
  469. for skipToken(p, TokenCOMMA) == nil {
  470. if err := acceptShowTerm(); err != nil {
  471. return nil, err
  472. }
  473. }
  474. }
  475. return self, nil
  476. }
  477. /*
  478. ndWith is used to parse a with clauses.
  479. */
  480. func ndWith(p *parser, self *ASTNode) (*ASTNode, error) {
  481. // Parse the rest and add it as children
  482. for p.node.Token.ID != TokenEOF {
  483. exp, err := p.run(0)
  484. if err != nil {
  485. return nil, err
  486. }
  487. self.Children = append(self.Children, exp)
  488. if p.node.Token.ID == TokenCOMMA {
  489. skipToken(p, TokenCOMMA)
  490. }
  491. }
  492. return self, nil
  493. }
  494. /*
  495. ndWithFunc is used to parse directives in with clauses.
  496. */
  497. func ndWithFunc(p *parser, self *ASTNode) (*ASTNode, error) {
  498. // Must have an opening bracket
  499. if err := skipToken(p, TokenLPAREN); err != nil {
  500. return nil, err
  501. }
  502. for p.node.Token.ID != TokenRPAREN {
  503. // Parse all the expressions inside the directives
  504. exp, err := p.run(0)
  505. if err != nil {
  506. return nil, err
  507. }
  508. self.Children = append(self.Children, exp)
  509. if p.node.Token.ID == TokenCOMMA {
  510. skipToken(p, TokenCOMMA)
  511. }
  512. }
  513. // Must have a closing bracket
  514. return self, skipToken(p, TokenRPAREN)
  515. }
  516. /*
  517. ndList is used to collect elements of a list.
  518. */
  519. func ndList(p *parser, self *ASTNode) (*ASTNode, error) {
  520. // Create a list token
  521. st := astNodeMap[TokenLIST].instance(p, self.Token)
  522. // Get the inner expression
  523. for p.node.Token.ID != TokenRBRACK {
  524. // Parse all the expressions inside the directives
  525. exp, err := p.run(0)
  526. if err != nil {
  527. return nil, err
  528. }
  529. st.Children = append(st.Children, exp)
  530. if p.node.Token.ID == TokenCOMMA {
  531. skipToken(p, TokenCOMMA)
  532. }
  533. }
  534. // Must have a closing bracket
  535. return st, skipToken(p, TokenRBRACK)
  536. }
  537. // Standard left denotation functions
  538. // ==================================
  539. /*
  540. ldInfix is used for infix operators.
  541. */
  542. func ldInfix(p *parser, self *ASTNode, left *ASTNode) (*ASTNode, error) {
  543. right, err := p.run(self.binding)
  544. if err != nil {
  545. return nil, err
  546. }
  547. self.Children = append(self.Children, left)
  548. self.Children = append(self.Children, right)
  549. return self, nil
  550. }
  551. // Helper functions
  552. // ================
  553. /*
  554. skipToken skips over a given token.
  555. */
  556. func skipToken(p *parser, ids ...LexTokenID) error {
  557. var err error
  558. canSkip := func(id LexTokenID) bool {
  559. for _, i := range ids {
  560. if i == id {
  561. return true
  562. }
  563. }
  564. return false
  565. }
  566. if !canSkip(p.node.Token.ID) {
  567. if p.node.Token.ID == TokenEOF {
  568. return p.newParserError(ErrUnexpectedEnd, "", *p.node.Token)
  569. }
  570. return p.newParserError(ErrUnexpectedToken, p.node.Token.Val, *p.node.Token)
  571. }
  572. // This should never return an error unless we skip over EOF or complex tokens
  573. // like values
  574. p.node, err = p.next()
  575. return err
  576. }
  577. /*
  578. acceptChild accepts the current token as a child.
  579. */
  580. func acceptChild(p *parser, self *ASTNode, id LexTokenID) error {
  581. var err error
  582. current := p.node
  583. p.node, err = p.next()
  584. if err != nil {
  585. return err
  586. }
  587. if current.Token.ID == id {
  588. self.Children = append(self.Children, current)
  589. return nil
  590. }
  591. return p.newParserError(ErrUnexpectedToken, current.Token.Val, *current.Token)
  592. }