parser.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package parser
  10. import (
  11. "fmt"
  12. )
  13. /*
  14. Map of AST nodes corresponding to lexer tokens. The map determines how a given
  15. sequence of lexer tokens are organized into an AST.
  16. */
  17. var astNodeMap map[LexTokenID]*ASTNode
  18. func init() {
  19. astNodeMap = map[LexTokenID]*ASTNode{
  20. TokenEOF: {NodeEOF, nil, nil, nil, nil, 0, ndTerm, nil},
  21. // Value tokens
  22. TokenSTRING: {NodeSTRING, nil, nil, nil, nil, 0, ndTerm, nil},
  23. TokenNUMBER: {NodeNUMBER, nil, nil, nil, nil, 0, ndTerm, nil},
  24. TokenIDENTIFIER: {NodeIDENTIFIER, nil, nil, nil, nil, 0, ndIdentifier, nil},
  25. // Constructed tokens
  26. TokenSTATEMENTS: {NodeSTATEMENTS, nil, nil, nil, nil, 0, nil, nil},
  27. TokenFUNCCALL: {NodeFUNCCALL, nil, nil, nil, nil, 0, nil, nil},
  28. TokenCOMPACCESS: {NodeCOMPACCESS, nil, nil, nil, nil, 0, nil, nil},
  29. TokenLIST: {NodeLIST, nil, nil, nil, nil, 0, nil, nil},
  30. TokenMAP: {NodeMAP, nil, nil, nil, nil, 0, nil, nil},
  31. TokenPARAMS: {NodePARAMS, nil, nil, nil, nil, 0, nil, nil},
  32. TokenGUARD: {NodeGUARD, nil, nil, nil, nil, 0, nil, nil},
  33. // Condition operators
  34. TokenGEQ: {NodeGEQ, nil, nil, nil, nil, 60, nil, ldInfix},
  35. TokenLEQ: {NodeLEQ, nil, nil, nil, nil, 60, nil, ldInfix},
  36. TokenNEQ: {NodeNEQ, nil, nil, nil, nil, 60, nil, ldInfix},
  37. TokenEQ: {NodeEQ, nil, nil, nil, nil, 60, nil, ldInfix},
  38. TokenGT: {NodeGT, nil, nil, nil, nil, 60, nil, ldInfix},
  39. TokenLT: {NodeLT, nil, nil, nil, nil, 60, nil, ldInfix},
  40. // Grouping symbols
  41. TokenLPAREN: {"", nil, nil, nil, nil, 150, ndInner, nil},
  42. TokenRPAREN: {"", nil, nil, nil, nil, 0, nil, nil},
  43. TokenLBRACK: {"", nil, nil, nil, nil, 150, ndList, nil},
  44. TokenRBRACK: {"", nil, nil, nil, nil, 0, nil, nil},
  45. TokenLBRACE: {"", nil, nil, nil, nil, 150, ndMap, nil},
  46. TokenRBRACE: {"", nil, nil, nil, nil, 0, nil, nil},
  47. // Separators
  48. TokenDOT: {"", nil, nil, nil, nil, 0, nil, nil},
  49. TokenCOMMA: {"", nil, nil, nil, nil, 0, nil, nil},
  50. TokenSEMICOLON: {"", nil, nil, nil, nil, 0, nil, nil},
  51. // Grouping
  52. TokenCOLON: {NodeKVP, nil, nil, nil, nil, 60, nil, ldInfix},
  53. TokenEQUAL: {NodePRESET, nil, nil, nil, nil, 60, nil, ldInfix},
  54. // Arithmetic operators
  55. TokenPLUS: {NodePLUS, nil, nil, nil, nil, 110, ndPrefix, ldInfix},
  56. TokenMINUS: {NodeMINUS, nil, nil, nil, nil, 110, ndPrefix, ldInfix},
  57. TokenTIMES: {NodeTIMES, nil, nil, nil, nil, 120, nil, ldInfix},
  58. TokenDIV: {NodeDIV, nil, nil, nil, nil, 120, nil, ldInfix},
  59. TokenDIVINT: {NodeDIVINT, nil, nil, nil, nil, 120, nil, ldInfix},
  60. TokenMODINT: {NodeMODINT, nil, nil, nil, nil, 120, nil, ldInfix},
  61. // Assignment statement
  62. TokenASSIGN: {NodeASSIGN, nil, nil, nil, nil, 10, nil, ldInfix},
  63. // Import statement
  64. TokenIMPORT: {NodeIMPORT, nil, nil, nil, nil, 0, ndImport, nil},
  65. TokenAS: {"", nil, nil, nil, nil, 0, ndImport, nil},
  66. // Sink definition
  67. TokenSINK: {NodeSINK, nil, nil, nil, nil, 0, ndSkink, nil},
  68. TokenKINDMATCH: {NodeKINDMATCH, nil, nil, nil, nil, 150, ndPrefix, nil},
  69. TokenSCOPEMATCH: {NodeSCOPEMATCH, nil, nil, nil, nil, 150, ndPrefix, nil},
  70. TokenSTATEMATCH: {NodeSTATEMATCH, nil, nil, nil, nil, 150, ndPrefix, nil},
  71. TokenPRIORITY: {NodePRIORITY, nil, nil, nil, nil, 150, ndPrefix, nil},
  72. TokenSUPPRESSES: {NodeSUPPRESSES, nil, nil, nil, nil, 150, ndPrefix, nil},
  73. // Function definition
  74. TokenFUNC: {NodeFUNC, nil, nil, nil, nil, 0, ndFunc, nil},
  75. TokenRETURN: {NodeRETURN, nil, nil, nil, nil, 0, ndReturn, nil},
  76. // Boolean operators
  77. TokenAND: {NodeAND, nil, nil, nil, nil, 40, nil, ldInfix},
  78. TokenOR: {NodeOR, nil, nil, nil, nil, 30, nil, ldInfix},
  79. TokenNOT: {NodeNOT, nil, nil, nil, nil, 20, ndPrefix, nil},
  80. // Condition operators
  81. TokenLIKE: {NodeLIKE, nil, nil, nil, nil, 60, nil, ldInfix},
  82. TokenIN: {NodeIN, nil, nil, nil, nil, 60, nil, ldInfix},
  83. TokenHASPREFIX: {NodeHASPREFIX, nil, nil, nil, nil, 60, nil, ldInfix},
  84. TokenHASSUFFIX: {NodeHASSUFFIX, nil, nil, nil, nil, 60, nil, ldInfix},
  85. TokenNOTIN: {NodeNOTIN, nil, nil, nil, nil, 60, nil, ldInfix},
  86. // Constant terminals
  87. TokenFALSE: {NodeFALSE, nil, nil, nil, nil, 0, ndTerm, nil},
  88. TokenTRUE: {NodeTRUE, nil, nil, nil, nil, 0, ndTerm, nil},
  89. TokenNULL: {NodeNULL, nil, nil, nil, nil, 0, ndTerm, nil},
  90. // Conditional statements
  91. TokenIF: {NodeIF, nil, nil, nil, nil, 0, ndGuard, nil},
  92. TokenELIF: {"", nil, nil, nil, nil, 0, nil, nil},
  93. TokenELSE: {"", nil, nil, nil, nil, 0, nil, nil},
  94. // Loop statements
  95. TokenFOR: {NodeLOOP, nil, nil, nil, nil, 0, ndLoop, nil},
  96. TokenBREAK: {NodeBREAK, nil, nil, nil, nil, 0, ndTerm, nil},
  97. TokenCONTINUE: {NodeCONTINUE, nil, nil, nil, nil, 0, ndTerm, nil},
  98. }
  99. }
  100. // Parser
  101. // ======
  102. /*
  103. Parser data structure
  104. */
  105. type parser struct {
  106. name string // Name to identify the input
  107. node *ASTNode // Current ast node
  108. tokens *LABuffer // Buffer which is connected to the channel which contains lex tokens
  109. rp RuntimeProvider // Runtime provider which creates runtime components
  110. }
  111. /*
  112. Parse parses a given input string and returns an AST.
  113. */
  114. func Parse(name string, input string) (*ASTNode, error) {
  115. return ParseWithRuntime(name, input, nil)
  116. }
  117. /*
  118. ParseWithRuntime parses a given input string and returns an AST decorated with
  119. runtime components.
  120. */
  121. func ParseWithRuntime(name string, input string, rp RuntimeProvider) (*ASTNode, error) {
  122. // Create a new parser with a look-ahead buffer of 3
  123. p := &parser{name, nil, NewLABuffer(Lex(name, input), 3), rp}
  124. // Read and set initial AST node
  125. node, err := p.next()
  126. if err != nil {
  127. return nil, err
  128. }
  129. p.node = node
  130. n, err := p.run(0)
  131. if err == nil && hasMoreStatements(p, n) {
  132. st := astNodeMap[TokenSTATEMENTS].instance(p, nil)
  133. st.Children = append(st.Children, n)
  134. for err == nil && hasMoreStatements(p, n) {
  135. // Skip semicolons
  136. if p.node.Token.ID == TokenSEMICOLON {
  137. skipToken(p, TokenSEMICOLON)
  138. }
  139. n, err = p.run(0)
  140. st.Children = append(st.Children, n)
  141. }
  142. n = st
  143. }
  144. if err == nil && p.node != nil && p.node.Token.ID != TokenEOF {
  145. token := *p.node.Token
  146. err = p.newParserError(ErrUnexpectedEnd, fmt.Sprintf("extra token id:%v (%v)",
  147. token.ID, token), token)
  148. }
  149. return n, err
  150. }
  151. /*
  152. run models the main parser function.
  153. */
  154. func (p *parser) run(rightBinding int) (*ASTNode, error) {
  155. var err error
  156. n := p.node
  157. p.node, err = p.next()
  158. if err != nil {
  159. return nil, err
  160. }
  161. // Start with the null denotation of this statement / expression
  162. if n.nullDenotation == nil {
  163. return nil, p.newParserError(ErrImpossibleNullDenotation,
  164. n.Token.String(), *n.Token)
  165. }
  166. left, err := n.nullDenotation(p, n)
  167. if err != nil {
  168. return nil, err
  169. }
  170. // Collect left denotations as long as the left binding power is greater
  171. // than the initial right one
  172. for rightBinding < p.node.binding {
  173. var nleft *ASTNode
  174. n = p.node
  175. if n.leftDenotation == nil {
  176. if left.Token.Lline < n.Token.Lline {
  177. // If the impossible left denotation is on a new line
  178. // we might be parsing a new statement
  179. return left, nil
  180. }
  181. return nil, p.newParserError(ErrImpossibleLeftDenotation,
  182. n.Token.String(), *n.Token)
  183. }
  184. p.node, err = p.next()
  185. if err != nil {
  186. return nil, err
  187. }
  188. // Get the next left denotation
  189. nleft, err = n.leftDenotation(p, n, left)
  190. left = nleft
  191. if err != nil {
  192. return nil, err
  193. }
  194. }
  195. return left, nil
  196. }
  197. /*
  198. next retrieves the next lexer token.
  199. */
  200. func (p *parser) next() (*ASTNode, error) {
  201. var preComments []MetaData
  202. var postComments []MetaData
  203. token, more := p.tokens.Next()
  204. // Skip over pre comment token
  205. for more && token.ID == TokenPRECOMMENT {
  206. preComments = append(preComments, NewLexTokenInstance(token))
  207. token, more = p.tokens.Next()
  208. }
  209. // Skip over post comment token
  210. for more && token.ID == TokenPOSTCOMMENT {
  211. postComments = append(postComments, NewLexTokenInstance(token))
  212. token, more = p.tokens.Next()
  213. }
  214. if !more {
  215. // Unexpected end of input - the associated token is an empty error token
  216. return nil, p.newParserError(ErrUnexpectedEnd, "", token)
  217. } else if token.ID == TokenError {
  218. // There was a lexer error wrap it in a parser error
  219. return nil, p.newParserError(ErrLexicalError, token.Val, token)
  220. } else if node, ok := astNodeMap[token.ID]; ok {
  221. // We got a normal AST component
  222. ret := node.instance(p, &token)
  223. ret.Meta = append(ret.Meta, preComments...) // Attach pre comments to the next AST node
  224. if len(postComments) > 0 && p.node != nil {
  225. p.node.Meta = append(p.node.Meta, postComments...) // Attach post comments to the previous AST node
  226. }
  227. return ret, nil
  228. }
  229. return nil, p.newParserError(ErrUnknownToken, fmt.Sprintf("id:%v (%v)", token.ID, token), token)
  230. }
  231. // Standard null denotation functions
  232. // ==================================
  233. /*
  234. ndTerm is used for terminals.
  235. */
  236. func ndTerm(p *parser, self *ASTNode) (*ASTNode, error) {
  237. return self, nil
  238. }
  239. /*
  240. ndInner returns the inner expression of an enclosed block and discard the
  241. block token. This method is used for brackets.
  242. */
  243. func ndInner(p *parser, self *ASTNode) (*ASTNode, error) {
  244. // Get the inner expression
  245. exp, err := p.run(0)
  246. if err != nil {
  247. return nil, err
  248. }
  249. // We return here the inner expression - discarding the bracket tokens
  250. return exp, skipToken(p, TokenRPAREN)
  251. }
  252. /*
  253. ndPrefix is used for prefix operators.
  254. */
  255. func ndPrefix(p *parser, self *ASTNode) (*ASTNode, error) {
  256. // Make sure a prefix will only prefix the next item
  257. val, err := p.run(self.binding + 20)
  258. if err != nil {
  259. return nil, err
  260. }
  261. self.Children = append(self.Children, val)
  262. return self, nil
  263. }
  264. // Null denotation functions for specific expressions
  265. // ==================================================
  266. /*
  267. ndImport is used to parse imports.
  268. */
  269. func ndImport(p *parser, self *ASTNode) (*ASTNode, error) {
  270. // Must specify a file path
  271. err := acceptChild(p, self, TokenSTRING)
  272. if err == nil {
  273. // Must specify AS
  274. if err = skipToken(p, TokenAS); err == nil {
  275. // Must specify an identifier
  276. err = acceptChild(p, self, TokenIDENTIFIER)
  277. }
  278. }
  279. return self, err
  280. }
  281. /*
  282. ndSink is used to parse sinks.
  283. */
  284. func ndSkink(p *parser, self *ASTNode) (*ASTNode, error) {
  285. var exp, ret *ASTNode
  286. // Must specify a name
  287. err := acceptChild(p, self, TokenIDENTIFIER)
  288. if err == nil {
  289. // Parse the rest of the parameters as children until we reach the body
  290. for err == nil && IsNotEndAndNotToken(p, TokenLBRACE) {
  291. if exp, err = p.run(150); err == nil {
  292. self.Children = append(self.Children, exp)
  293. // Skip commas
  294. if p.node.Token.ID == TokenCOMMA {
  295. err = skipToken(p, TokenCOMMA)
  296. }
  297. }
  298. }
  299. if err == nil {
  300. // Parse the body
  301. ret, err = parseInnerStatements(p, self)
  302. }
  303. }
  304. return ret, err
  305. }
  306. /*
  307. ndFunc is used to parse function definitions.
  308. */
  309. func ndFunc(p *parser, self *ASTNode) (*ASTNode, error) {
  310. var exp *ASTNode
  311. var err error
  312. // Might specify a function name
  313. if p.node.Token.ID == TokenIDENTIFIER {
  314. err = acceptChild(p, self, TokenIDENTIFIER)
  315. }
  316. // Read in parameters
  317. if err == nil {
  318. err = skipToken(p, TokenLPAREN)
  319. params := astNodeMap[TokenPARAMS].instance(p, nil)
  320. self.Children = append(self.Children, params)
  321. for err == nil && IsNotEndAndNotToken(p, TokenRPAREN) {
  322. // Parse all the expressions inside
  323. if exp, err = p.run(0); err == nil {
  324. params.Children = append(params.Children, exp)
  325. if p.node.Token.ID == TokenCOMMA {
  326. err = skipToken(p, TokenCOMMA)
  327. }
  328. }
  329. }
  330. if err == nil {
  331. err = skipToken(p, TokenRPAREN)
  332. }
  333. }
  334. if err == nil {
  335. // Parse the body
  336. self, err = parseInnerStatements(p, self)
  337. }
  338. return self, err
  339. }
  340. /*
  341. ndReturn is used to parse return statements.
  342. */
  343. func ndReturn(p *parser, self *ASTNode) (*ASTNode, error) {
  344. var err error
  345. if self.Token.Lline == p.node.Token.Lline {
  346. var val *ASTNode
  347. // Consume the next expression only if it is on the same line
  348. val, err = p.run(0)
  349. if err == nil {
  350. self.Children = append(self.Children, val)
  351. }
  352. }
  353. return self, err
  354. }
  355. /*
  356. ndIdentifier is to parse identifiers and function calls.
  357. */
  358. func ndIdentifier(p *parser, self *ASTNode) (*ASTNode, error) {
  359. var parseMore, parseSegment, parseFuncCall, parseCompositionAccess func(parent *ASTNode) error
  360. parseMore = func(current *ASTNode) error {
  361. var err error
  362. if p.node.Token.ID == TokenDOT {
  363. err = parseSegment(current)
  364. } else if p.node.Token.ID == TokenLPAREN {
  365. err = parseFuncCall(current)
  366. } else if p.node.Token.ID == TokenLBRACK && p.node.Token.Lline == self.Token.Lline {
  367. // Composition access needs to be on the same line as the identifier
  368. // as we might otherwise have a list
  369. err = parseCompositionAccess(current)
  370. }
  371. return err
  372. }
  373. parseSegment = func(current *ASTNode) error {
  374. var err error
  375. var next *ASTNode
  376. if err = skipToken(p, TokenDOT); err == nil {
  377. next = p.node
  378. if err = acceptChild(p, current, TokenIDENTIFIER); err == nil {
  379. err = parseMore(next)
  380. }
  381. }
  382. return err
  383. }
  384. parseFuncCall = func(current *ASTNode) error {
  385. var exp *ASTNode
  386. err := skipToken(p, TokenLPAREN)
  387. fc := astNodeMap[TokenFUNCCALL].instance(p, nil)
  388. current.Children = append(current.Children, fc)
  389. // Read in parameters
  390. for err == nil && IsNotEndAndNotToken(p, TokenRPAREN) {
  391. // Parse all the expressions inside the directives
  392. if exp, err = p.run(0); err == nil {
  393. fc.Children = append(fc.Children, exp)
  394. if p.node.Token.ID == TokenCOMMA {
  395. err = skipToken(p, TokenCOMMA)
  396. }
  397. }
  398. }
  399. if err == nil {
  400. err = skipToken(p, TokenRPAREN)
  401. if err == nil {
  402. err = parseMore(current)
  403. }
  404. }
  405. return err
  406. }
  407. parseCompositionAccess = func(current *ASTNode) error {
  408. var exp *ASTNode
  409. err := skipToken(p, TokenLBRACK)
  410. if err == nil {
  411. ca := astNodeMap[TokenCOMPACCESS].instance(p, nil)
  412. current.Children = append(current.Children, ca)
  413. // Parse all the expressions inside the directives
  414. if exp, err = p.run(0); err == nil {
  415. ca.Children = append(ca.Children, exp)
  416. if err = skipToken(p, TokenRBRACK); err == nil {
  417. err = parseMore(current)
  418. }
  419. }
  420. }
  421. return err
  422. }
  423. return self, parseMore(self)
  424. }
  425. /*
  426. ndList is used to collect elements of a list.
  427. */
  428. func ndList(p *parser, self *ASTNode) (*ASTNode, error) {
  429. var err error
  430. var exp *ASTNode
  431. // Create a list token
  432. st := astNodeMap[TokenLIST].instance(p, self.Token)
  433. // Get the inner expression
  434. for err == nil && IsNotEndAndNotToken(p, TokenRBRACK) {
  435. // Parse all the expressions inside
  436. if exp, err = p.run(0); err == nil {
  437. st.Children = append(st.Children, exp)
  438. if p.node.Token.ID == TokenCOMMA {
  439. err = skipToken(p, TokenCOMMA)
  440. }
  441. }
  442. }
  443. if err == nil {
  444. err = skipToken(p, TokenRBRACK)
  445. }
  446. // Must have a closing bracket
  447. return st, err
  448. }
  449. /*
  450. ndMap is used to collect elements of a map.
  451. */
  452. func ndMap(p *parser, self *ASTNode) (*ASTNode, error) {
  453. var err error
  454. var exp *ASTNode
  455. // Create a map token
  456. st := astNodeMap[TokenMAP].instance(p, self.Token)
  457. // Get the inner expression
  458. for err == nil && IsNotEndAndNotToken(p, TokenRBRACE) {
  459. // Parse all the expressions inside
  460. if exp, err = p.run(0); err == nil {
  461. st.Children = append(st.Children, exp)
  462. if p.node.Token.ID == TokenCOMMA {
  463. err = skipToken(p, TokenCOMMA)
  464. }
  465. }
  466. }
  467. if err == nil {
  468. err = skipToken(p, TokenRBRACE)
  469. }
  470. // Must have a closing brace
  471. return st, err
  472. }
  473. /*
  474. ndGuard is used to parse a conditional statement.
  475. */
  476. func ndGuard(p *parser, self *ASTNode) (*ASTNode, error) {
  477. var err error
  478. parseGuardAndStatements := func() error {
  479. // The brace starts statements while parsing the expression of an if statement
  480. nodeMapEntryBak := astNodeMap[TokenLBRACE]
  481. astNodeMap[TokenLBRACE] = &ASTNode{"", nil, nil, nil, nil, 0, parseInnerStatements, nil}
  482. exp, err := p.run(0)
  483. astNodeMap[TokenLBRACE] = nodeMapEntryBak
  484. if err == nil {
  485. g := astNodeMap[TokenGUARD].instance(p, nil)
  486. g.Children = append(g.Children, exp)
  487. self.Children = append(self.Children, g)
  488. _, err = parseInnerStatements(p, self)
  489. }
  490. return err
  491. }
  492. if err = parseGuardAndStatements(); err == nil {
  493. for err == nil && IsNotEndAndToken(p, TokenELIF) {
  494. // Parse an elif
  495. if err = skipToken(p, TokenELIF); err == nil {
  496. err = parseGuardAndStatements()
  497. }
  498. }
  499. if err == nil && p.node.Token.ID == TokenELSE {
  500. // Parse else
  501. if err = skipToken(p, TokenELSE); err == nil {
  502. g := astNodeMap[TokenGUARD].instance(p, nil)
  503. g.Children = append(g.Children, astNodeMap[TokenTRUE].instance(p, nil))
  504. self.Children = append(self.Children, g)
  505. _, err = parseInnerStatements(p, self)
  506. }
  507. }
  508. }
  509. return self, err
  510. }
  511. /*
  512. ndLoop is used to parse a loop statement.
  513. */
  514. func ndLoop(p *parser, self *ASTNode) (*ASTNode, error) {
  515. // The brace starts statements while parsing the expression of a for statement
  516. nodeMapEntryBak := astNodeMap[TokenLBRACE]
  517. astNodeMap[TokenLBRACE] = &ASTNode{"", nil, nil, nil, nil, 0, parseInnerStatements, nil}
  518. exp, err := p.run(0)
  519. astNodeMap[TokenLBRACE] = nodeMapEntryBak
  520. if err == nil {
  521. g := exp
  522. if exp.Token.ID != TokenIN {
  523. g = astNodeMap[TokenGUARD].instance(p, nil)
  524. g.Children = append(g.Children, exp)
  525. }
  526. self.Children = append(self.Children, g)
  527. _, err = parseInnerStatements(p, self)
  528. }
  529. return self, err
  530. }
  531. // Standard left denotation functions
  532. // ==================================
  533. /*
  534. ldInfix is used for infix operators.
  535. */
  536. func ldInfix(p *parser, self *ASTNode, left *ASTNode) (*ASTNode, error) {
  537. right, err := p.run(self.binding)
  538. if err != nil {
  539. return nil, err
  540. }
  541. self.Children = append(self.Children, left)
  542. self.Children = append(self.Children, right)
  543. return self, nil
  544. }
  545. // Helper functions
  546. // ================
  547. /*
  548. IsNotEndAndToken checks if the next token is of a specific type or the end has been reached.
  549. */
  550. func IsNotEndAndToken(p *parser, i LexTokenID) bool {
  551. return p.node != nil && p.node.Name != NodeEOF && p.node.Token.ID == i
  552. }
  553. /*
  554. IsNotEndAndNotToken checks if the next token is not of a specific type or the end has been reached.
  555. */
  556. func IsNotEndAndNotToken(p *parser, i LexTokenID) bool {
  557. return p.node != nil && p.node.Name != NodeEOF && p.node.Token.ID != i
  558. }
  559. /*
  560. hasMoreStatements returns true if there are more statements to parse.
  561. */
  562. func hasMoreStatements(p *parser, currentNode *ASTNode) bool {
  563. nextNode := p.node
  564. if nextNode == nil || nextNode.Token.ID == TokenEOF {
  565. return false
  566. } else if nextNode.Token.ID == TokenSEMICOLON {
  567. return true
  568. }
  569. return currentNode != nil && currentNode.Token.Lline < nextNode.Token.Lline
  570. }
  571. /*
  572. skipToken skips over a given token.
  573. */
  574. func skipToken(p *parser, ids ...LexTokenID) error {
  575. var err error
  576. canSkip := func(id LexTokenID) bool {
  577. for _, i := range ids {
  578. if i == id {
  579. return true
  580. }
  581. }
  582. return false
  583. }
  584. if !canSkip(p.node.Token.ID) {
  585. if p.node.Token.ID == TokenEOF {
  586. return p.newParserError(ErrUnexpectedEnd, "", *p.node.Token)
  587. }
  588. return p.newParserError(ErrUnexpectedToken, p.node.Token.Val, *p.node.Token)
  589. }
  590. // This should never return an error unless we skip over EOF or complex tokens
  591. // like values
  592. p.node, err = p.next()
  593. return err
  594. }
  595. /*
  596. acceptChild accepts the current token as a child.
  597. */
  598. func acceptChild(p *parser, self *ASTNode, id LexTokenID) error {
  599. var err error
  600. current := p.node
  601. if p.node, err = p.next(); err == nil {
  602. if current.Token.ID == id {
  603. self.Children = append(self.Children, current)
  604. } else {
  605. err = p.newParserError(ErrUnexpectedToken, current.Token.Val, *current.Token)
  606. }
  607. }
  608. return err
  609. }
  610. /*
  611. parseInnerStatements collects the inner statements of a block statement. It
  612. is assumed that a block statement starts with a left brace '{' and ends with
  613. a right brace '}'.
  614. */
  615. func parseInnerStatements(p *parser, self *ASTNode) (*ASTNode, error) {
  616. // Must start with an opening brace
  617. if err := skipToken(p, TokenLBRACE); err != nil {
  618. return nil, err
  619. }
  620. // Always create a statements node
  621. st := astNodeMap[TokenSTATEMENTS].instance(p, nil)
  622. self.Children = append(self.Children, st)
  623. // Check if there are actually children
  624. if p.node != nil && p.node.Token.ID != TokenRBRACE {
  625. n, err := p.run(0)
  626. if p.node != nil && p.node.Token.ID != TokenEOF {
  627. st.Children = append(st.Children, n)
  628. for hasMoreStatements(p, n) {
  629. if p.node.Token.ID == TokenSEMICOLON {
  630. skipToken(p, TokenSEMICOLON)
  631. } else if p.node.Token.ID == TokenRBRACE {
  632. break
  633. }
  634. n, err = p.run(0)
  635. st.Children = append(st.Children, n)
  636. }
  637. }
  638. if err != nil {
  639. return nil, err
  640. }
  641. }
  642. // Must end with a closing brace
  643. return self, skipToken(p, TokenRBRACE)
  644. }