parser.go 22 KB

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