grammar.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  1. // Code generated by pigeon; DO NOT EDIT.
  2. package grammar
  3. import (
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "math"
  10. "os"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "unicode"
  16. "unicode/utf8"
  17. )
  18. var g = &grammar{
  19. rules: []*rule{
  20. {
  21. name: "Input",
  22. pos: position{line: 6, col: 1, offset: 22},
  23. expr: &oneOrMoreExpr{
  24. pos: position{line: 6, col: 9, offset: 30},
  25. expr: &choiceExpr{
  26. pos: position{line: 6, col: 10, offset: 31},
  27. alternatives: []interface{}{
  28. &ruleRefExpr{
  29. pos: position{line: 6, col: 10, offset: 31},
  30. name: "Std",
  31. },
  32. &ruleRefExpr{
  33. pos: position{line: 6, col: 16, offset: 37},
  34. name: "EOL",
  35. },
  36. },
  37. },
  38. },
  39. },
  40. {
  41. name: "LabelUpper",
  42. pos: position{line: 8, col: 1, offset: 44},
  43. expr: &actionExpr{
  44. pos: position{line: 8, col: 14, offset: 57},
  45. run: (*parser).callonLabelUpper1,
  46. expr: &oneOrMoreExpr{
  47. pos: position{line: 8, col: 14, offset: 57},
  48. expr: &charClassMatcher{
  49. pos: position{line: 8, col: 14, offset: 57},
  50. val: "[A-Z_]",
  51. chars: []rune{'_'},
  52. ranges: []rune{'A', 'Z'},
  53. ignoreCase: false,
  54. inverted: false,
  55. },
  56. },
  57. },
  58. },
  59. {
  60. name: "Std",
  61. pos: position{line: 13, col: 1, offset: 119},
  62. expr: &seqExpr{
  63. pos: position{line: 13, col: 7, offset: 125},
  64. exprs: []interface{}{
  65. &ruleRefExpr{
  66. pos: position{line: 13, col: 7, offset: 125},
  67. name: "LabelUpperLine",
  68. },
  69. &choiceExpr{
  70. pos: position{line: 13, col: 23, offset: 141},
  71. alternatives: []interface{}{
  72. &oneOrMoreExpr{
  73. pos: position{line: 13, col: 23, offset: 141},
  74. expr: &ruleRefExpr{
  75. pos: position{line: 13, col: 23, offset: 141},
  76. name: "LabelLine",
  77. },
  78. },
  79. &oneOrMoreExpr{
  80. pos: position{line: 13, col: 36, offset: 154},
  81. expr: &ruleRefExpr{
  82. pos: position{line: 13, col: 36, offset: 154},
  83. name: "UpperLabelLine",
  84. },
  85. },
  86. },
  87. },
  88. },
  89. },
  90. },
  91. {
  92. name: "LabelUpperLine",
  93. pos: position{line: 15, col: 1, offset: 172},
  94. expr: &seqExpr{
  95. pos: position{line: 15, col: 18, offset: 189},
  96. exprs: []interface{}{
  97. &ruleRefExpr{
  98. pos: position{line: 15, col: 18, offset: 189},
  99. name: "LabelUpper",
  100. },
  101. &ruleRefExpr{
  102. pos: position{line: 15, col: 29, offset: 200},
  103. name: "EOL",
  104. },
  105. },
  106. },
  107. },
  108. {
  109. name: "LabelLine",
  110. pos: position{line: 16, col: 1, offset: 204},
  111. expr: &seqExpr{
  112. pos: position{line: 16, col: 13, offset: 216},
  113. exprs: []interface{}{
  114. &charClassMatcher{
  115. pos: position{line: 16, col: 13, offset: 216},
  116. val: "[\\t]",
  117. chars: []rune{'\t'},
  118. ignoreCase: false,
  119. inverted: false,
  120. },
  121. &ruleRefExpr{
  122. pos: position{line: 16, col: 18, offset: 221},
  123. name: "Label",
  124. },
  125. &ruleRefExpr{
  126. pos: position{line: 16, col: 24, offset: 227},
  127. name: "EOL",
  128. },
  129. },
  130. },
  131. },
  132. {
  133. name: "UpperLabelLine",
  134. pos: position{line: 17, col: 1, offset: 231},
  135. expr: &seqExpr{
  136. pos: position{line: 17, col: 18, offset: 248},
  137. exprs: []interface{}{
  138. &charClassMatcher{
  139. pos: position{line: 17, col: 18, offset: 248},
  140. val: "[\\t]",
  141. chars: []rune{'\t'},
  142. ignoreCase: false,
  143. inverted: false,
  144. },
  145. &oneOrMoreExpr{
  146. pos: position{line: 17, col: 23, offset: 253},
  147. expr: &seqExpr{
  148. pos: position{line: 17, col: 24, offset: 254},
  149. exprs: []interface{}{
  150. &ruleRefExpr{
  151. pos: position{line: 17, col: 24, offset: 254},
  152. name: "LabelUpper",
  153. },
  154. &ruleRefExpr{
  155. pos: position{line: 17, col: 35, offset: 265},
  156. name: "_",
  157. },
  158. },
  159. },
  160. },
  161. &ruleRefExpr{
  162. pos: position{line: 17, col: 39, offset: 269},
  163. name: "UpperLabel",
  164. },
  165. &ruleRefExpr{
  166. pos: position{line: 17, col: 50, offset: 280},
  167. name: "EOL",
  168. },
  169. },
  170. },
  171. },
  172. {
  173. name: "Label",
  174. pos: position{line: 19, col: 1, offset: 285},
  175. expr: &actionExpr{
  176. pos: position{line: 19, col: 9, offset: 293},
  177. run: (*parser).callonLabel1,
  178. expr: &oneOrMoreExpr{
  179. pos: position{line: 19, col: 9, offset: 293},
  180. expr: &charClassMatcher{
  181. pos: position{line: 19, col: 9, offset: 293},
  182. val: "[a-z_]",
  183. chars: []rune{'_'},
  184. ranges: []rune{'a', 'z'},
  185. ignoreCase: false,
  186. inverted: false,
  187. },
  188. },
  189. },
  190. },
  191. {
  192. name: "_",
  193. pos: position{line: 25, col: 1, offset: 356},
  194. expr: &oneOrMoreExpr{
  195. pos: position{line: 25, col: 5, offset: 360},
  196. expr: &charClassMatcher{
  197. pos: position{line: 25, col: 5, offset: 360},
  198. val: "[ \\t]",
  199. chars: []rune{' ', '\t'},
  200. ignoreCase: false,
  201. inverted: false,
  202. },
  203. },
  204. },
  205. {
  206. name: "EOL",
  207. pos: position{line: 27, col: 1, offset: 368},
  208. expr: &seqExpr{
  209. pos: position{line: 27, col: 7, offset: 374},
  210. exprs: []interface{}{
  211. &zeroOrOneExpr{
  212. pos: position{line: 27, col: 7, offset: 374},
  213. expr: &ruleRefExpr{
  214. pos: position{line: 27, col: 7, offset: 374},
  215. name: "_",
  216. },
  217. },
  218. &zeroOrOneExpr{
  219. pos: position{line: 27, col: 10, offset: 377},
  220. expr: &ruleRefExpr{
  221. pos: position{line: 27, col: 10, offset: 377},
  222. name: "Comment",
  223. },
  224. },
  225. &choiceExpr{
  226. pos: position{line: 27, col: 20, offset: 387},
  227. alternatives: []interface{}{
  228. &litMatcher{
  229. pos: position{line: 27, col: 20, offset: 387},
  230. val: "\r\n",
  231. ignoreCase: false,
  232. want: "\"\\r\\n\"",
  233. },
  234. &litMatcher{
  235. pos: position{line: 27, col: 29, offset: 396},
  236. val: "\n\r",
  237. ignoreCase: false,
  238. want: "\"\\n\\r\"",
  239. },
  240. &litMatcher{
  241. pos: position{line: 27, col: 38, offset: 405},
  242. val: "\r",
  243. ignoreCase: false,
  244. want: "\"\\r\"",
  245. },
  246. &litMatcher{
  247. pos: position{line: 27, col: 45, offset: 412},
  248. val: "\n",
  249. ignoreCase: false,
  250. want: "\"\\n\"",
  251. },
  252. &ruleRefExpr{
  253. pos: position{line: 27, col: 52, offset: 419},
  254. name: "EOF",
  255. },
  256. },
  257. },
  258. },
  259. },
  260. },
  261. {
  262. name: "EOF",
  263. pos: position{line: 29, col: 1, offset: 425},
  264. expr: &notExpr{
  265. pos: position{line: 29, col: 7, offset: 431},
  266. expr: &anyMatcher{
  267. line: 29, col: 8, offset: 432,
  268. },
  269. },
  270. },
  271. {
  272. name: "Comment",
  273. pos: position{line: 31, col: 1, offset: 435},
  274. expr: &seqExpr{
  275. pos: position{line: 31, col: 11, offset: 445},
  276. exprs: []interface{}{
  277. &litMatcher{
  278. pos: position{line: 31, col: 11, offset: 445},
  279. val: "//",
  280. ignoreCase: false,
  281. want: "\"//\"",
  282. },
  283. &zeroOrMoreExpr{
  284. pos: position{line: 31, col: 16, offset: 450},
  285. expr: &charClassMatcher{
  286. pos: position{line: 31, col: 16, offset: 450},
  287. val: "[^\\r\\n]",
  288. chars: []rune{'\r', '\n'},
  289. ignoreCase: false,
  290. inverted: true,
  291. },
  292. },
  293. },
  294. },
  295. },
  296. },
  297. }
  298. func (c *current) onLabelUpper1() (interface{}, error) {
  299. fmt.Println(string(c.text))
  300. return c.text, nil
  301. }
  302. func (p *parser) callonLabelUpper1() (interface{}, error) {
  303. stack := p.vstack[len(p.vstack)-1]
  304. _ = stack
  305. return p.cur.onLabelUpper1()
  306. }
  307. func (c *current) onLabel1() (interface{}, error) {
  308. fmt.Println(string(c.text))
  309. return c.text, nil
  310. }
  311. func (p *parser) callonLabel1() (interface{}, error) {
  312. stack := p.vstack[len(p.vstack)-1]
  313. _ = stack
  314. return p.cur.onLabel1()
  315. }
  316. var (
  317. // errNoRule is returned when the grammar to parse has no rule.
  318. errNoRule = errors.New("grammar has no rule")
  319. // errInvalidEntrypoint is returned when the specified entrypoint rule
  320. // does not exit.
  321. errInvalidEntrypoint = errors.New("invalid entrypoint")
  322. // errInvalidEncoding is returned when the source is not properly
  323. // utf8-encoded.
  324. errInvalidEncoding = errors.New("invalid encoding")
  325. // errMaxExprCnt is used to signal that the maximum number of
  326. // expressions have been parsed.
  327. errMaxExprCnt = errors.New("max number of expresssions parsed")
  328. )
  329. // Option is a function that can set an option on the parser. It returns
  330. // the previous setting as an Option.
  331. type Option func(*parser) Option
  332. // MaxExpressions creates an Option to stop parsing after the provided
  333. // number of expressions have been parsed, if the value is 0 then the parser will
  334. // parse for as many steps as needed (possibly an infinite number).
  335. //
  336. // The default for maxExprCnt is 0.
  337. func MaxExpressions(maxExprCnt uint64) Option {
  338. return func(p *parser) Option {
  339. oldMaxExprCnt := p.maxExprCnt
  340. p.maxExprCnt = maxExprCnt
  341. return MaxExpressions(oldMaxExprCnt)
  342. }
  343. }
  344. // Entrypoint creates an Option to set the rule name to use as entrypoint.
  345. // The rule name must have been specified in the -alternate-entrypoints
  346. // if generating the parser with the -optimize-grammar flag, otherwise
  347. // it may have been optimized out. Passing an empty string sets the
  348. // entrypoint to the first rule in the grammar.
  349. //
  350. // The default is to start parsing at the first rule in the grammar.
  351. func Entrypoint(ruleName string) Option {
  352. return func(p *parser) Option {
  353. oldEntrypoint := p.entrypoint
  354. p.entrypoint = ruleName
  355. if ruleName == "" {
  356. p.entrypoint = g.rules[0].name
  357. }
  358. return Entrypoint(oldEntrypoint)
  359. }
  360. }
  361. // Statistics adds a user provided Stats struct to the parser to allow
  362. // the user to process the results after the parsing has finished.
  363. // Also the key for the "no match" counter is set.
  364. //
  365. // Example usage:
  366. //
  367. // input := "input"
  368. // stats := Stats{}
  369. // _, err := Parse("input-file", []byte(input), Statistics(&stats, "no match"))
  370. // if err != nil {
  371. // log.Panicln(err)
  372. // }
  373. // b, err := json.MarshalIndent(stats.ChoiceAltCnt, "", " ")
  374. // if err != nil {
  375. // log.Panicln(err)
  376. // }
  377. // fmt.Println(string(b))
  378. func Statistics(stats *Stats, choiceNoMatch string) Option {
  379. return func(p *parser) Option {
  380. oldStats := p.Stats
  381. p.Stats = stats
  382. oldChoiceNoMatch := p.choiceNoMatch
  383. p.choiceNoMatch = choiceNoMatch
  384. if p.Stats.ChoiceAltCnt == nil {
  385. p.Stats.ChoiceAltCnt = make(map[string]map[string]int)
  386. }
  387. return Statistics(oldStats, oldChoiceNoMatch)
  388. }
  389. }
  390. // Debug creates an Option to set the debug flag to b. When set to true,
  391. // debugging information is printed to stdout while parsing.
  392. //
  393. // The default is false.
  394. func Debug(b bool) Option {
  395. return func(p *parser) Option {
  396. old := p.debug
  397. p.debug = b
  398. return Debug(old)
  399. }
  400. }
  401. // Memoize creates an Option to set the memoize flag to b. When set to true,
  402. // the parser will cache all results so each expression is evaluated only
  403. // once. This guarantees linear parsing time even for pathological cases,
  404. // at the expense of more memory and slower times for typical cases.
  405. //
  406. // The default is false.
  407. func Memoize(b bool) Option {
  408. return func(p *parser) Option {
  409. old := p.memoize
  410. p.memoize = b
  411. return Memoize(old)
  412. }
  413. }
  414. // AllowInvalidUTF8 creates an Option to allow invalid UTF-8 bytes.
  415. // Every invalid UTF-8 byte is treated as a utf8.RuneError (U+FFFD)
  416. // by character class matchers and is matched by the any matcher.
  417. // The returned matched value, c.text and c.offset are NOT affected.
  418. //
  419. // The default is false.
  420. func AllowInvalidUTF8(b bool) Option {
  421. return func(p *parser) Option {
  422. old := p.allowInvalidUTF8
  423. p.allowInvalidUTF8 = b
  424. return AllowInvalidUTF8(old)
  425. }
  426. }
  427. // Recover creates an Option to set the recover flag to b. When set to
  428. // true, this causes the parser to recover from panics and convert it
  429. // to an error. Setting it to false can be useful while debugging to
  430. // access the full stack trace.
  431. //
  432. // The default is true.
  433. func Recover(b bool) Option {
  434. return func(p *parser) Option {
  435. old := p.recover
  436. p.recover = b
  437. return Recover(old)
  438. }
  439. }
  440. // GlobalStore creates an Option to set a key to a certain value in
  441. // the globalStore.
  442. func GlobalStore(key string, value interface{}) Option {
  443. return func(p *parser) Option {
  444. old := p.cur.globalStore[key]
  445. p.cur.globalStore[key] = value
  446. return GlobalStore(key, old)
  447. }
  448. }
  449. // InitState creates an Option to set a key to a certain value in
  450. // the global "state" store.
  451. func InitState(key string, value interface{}) Option {
  452. return func(p *parser) Option {
  453. old := p.cur.state[key]
  454. p.cur.state[key] = value
  455. return InitState(key, old)
  456. }
  457. }
  458. // ParseFile parses the file identified by filename.
  459. func ParseFile(filename string, opts ...Option) (i interface{}, err error) {
  460. f, err := os.Open(filename)
  461. if err != nil {
  462. return nil, err
  463. }
  464. defer func() {
  465. if closeErr := f.Close(); closeErr != nil {
  466. err = closeErr
  467. }
  468. }()
  469. return ParseReader(filename, f, opts...)
  470. }
  471. // ParseReader parses the data from r using filename as information in the
  472. // error messages.
  473. func ParseReader(filename string, r io.Reader, opts ...Option) (interface{}, error) {
  474. b, err := ioutil.ReadAll(r)
  475. if err != nil {
  476. return nil, err
  477. }
  478. return Parse(filename, b, opts...)
  479. }
  480. // Parse parses the data from b using filename as information in the
  481. // error messages.
  482. func Parse(filename string, b []byte, opts ...Option) (interface{}, error) {
  483. return newParser(filename, b, opts...).parse(g)
  484. }
  485. // position records a position in the text.
  486. type position struct {
  487. line, col, offset int
  488. }
  489. func (p position) String() string {
  490. return strconv.Itoa(p.line) + ":" + strconv.Itoa(p.col) + " [" + strconv.Itoa(p.offset) + "]"
  491. }
  492. // savepoint stores all state required to go back to this point in the
  493. // parser.
  494. type savepoint struct {
  495. position
  496. rn rune
  497. w int
  498. }
  499. type current struct {
  500. pos position // start position of the match
  501. text []byte // raw text of the match
  502. // state is a store for arbitrary key,value pairs that the user wants to be
  503. // tied to the backtracking of the parser.
  504. // This is always rolled back if a parsing rule fails.
  505. state storeDict
  506. // globalStore is a general store for the user to store arbitrary key-value
  507. // pairs that they need to manage and that they do not want tied to the
  508. // backtracking of the parser. This is only modified by the user and never
  509. // rolled back by the parser. It is always up to the user to keep this in a
  510. // consistent state.
  511. globalStore storeDict
  512. }
  513. type storeDict map[string]interface{}
  514. // the AST types...
  515. type grammar struct {
  516. pos position
  517. rules []*rule
  518. }
  519. type rule struct {
  520. pos position
  521. name string
  522. displayName string
  523. expr interface{}
  524. }
  525. type choiceExpr struct {
  526. pos position
  527. alternatives []interface{}
  528. }
  529. type actionExpr struct {
  530. pos position
  531. expr interface{}
  532. run func(*parser) (interface{}, error)
  533. }
  534. type recoveryExpr struct {
  535. pos position
  536. expr interface{}
  537. recoverExpr interface{}
  538. failureLabel []string
  539. }
  540. type seqExpr struct {
  541. pos position
  542. exprs []interface{}
  543. }
  544. type throwExpr struct {
  545. pos position
  546. label string
  547. }
  548. type labeledExpr struct {
  549. pos position
  550. label string
  551. expr interface{}
  552. }
  553. type expr struct {
  554. pos position
  555. expr interface{}
  556. }
  557. type andExpr expr
  558. type notExpr expr
  559. type zeroOrOneExpr expr
  560. type zeroOrMoreExpr expr
  561. type oneOrMoreExpr expr
  562. type ruleRefExpr struct {
  563. pos position
  564. name string
  565. }
  566. type stateCodeExpr struct {
  567. pos position
  568. run func(*parser) error
  569. }
  570. type andCodeExpr struct {
  571. pos position
  572. run func(*parser) (bool, error)
  573. }
  574. type notCodeExpr struct {
  575. pos position
  576. run func(*parser) (bool, error)
  577. }
  578. type litMatcher struct {
  579. pos position
  580. val string
  581. ignoreCase bool
  582. want string
  583. }
  584. type charClassMatcher struct {
  585. pos position
  586. val string
  587. basicLatinChars [128]bool
  588. chars []rune
  589. ranges []rune
  590. classes []*unicode.RangeTable
  591. ignoreCase bool
  592. inverted bool
  593. }
  594. type anyMatcher position
  595. // errList cumulates the errors found by the parser.
  596. type errList []error
  597. func (e *errList) add(err error) {
  598. *e = append(*e, err)
  599. }
  600. func (e errList) err() error {
  601. if len(e) == 0 {
  602. return nil
  603. }
  604. e.dedupe()
  605. return e
  606. }
  607. func (e *errList) dedupe() {
  608. var cleaned []error
  609. set := make(map[string]bool)
  610. for _, err := range *e {
  611. if msg := err.Error(); !set[msg] {
  612. set[msg] = true
  613. cleaned = append(cleaned, err)
  614. }
  615. }
  616. *e = cleaned
  617. }
  618. func (e errList) Error() string {
  619. switch len(e) {
  620. case 0:
  621. return ""
  622. case 1:
  623. return e[0].Error()
  624. default:
  625. var buf bytes.Buffer
  626. for i, err := range e {
  627. if i > 0 {
  628. buf.WriteRune('\n')
  629. }
  630. buf.WriteString(err.Error())
  631. }
  632. return buf.String()
  633. }
  634. }
  635. // parserError wraps an error with a prefix indicating the rule in which
  636. // the error occurred. The original error is stored in the Inner field.
  637. type parserError struct {
  638. Inner error
  639. pos position
  640. prefix string
  641. expected []string
  642. }
  643. // Error returns the error message.
  644. func (p *parserError) Error() string {
  645. return p.prefix + ": " + p.Inner.Error()
  646. }
  647. // newParser creates a parser with the specified input source and options.
  648. func newParser(filename string, b []byte, opts ...Option) *parser {
  649. stats := Stats{
  650. ChoiceAltCnt: make(map[string]map[string]int),
  651. }
  652. p := &parser{
  653. filename: filename,
  654. errs: new(errList),
  655. data: b,
  656. pt: savepoint{position: position{line: 1}},
  657. recover: true,
  658. cur: current{
  659. state: make(storeDict),
  660. globalStore: make(storeDict),
  661. },
  662. maxFailPos: position{col: 1, line: 1},
  663. maxFailExpected: make([]string, 0, 20),
  664. Stats: &stats,
  665. // start rule is rule [0] unless an alternate entrypoint is specified
  666. entrypoint: g.rules[0].name,
  667. }
  668. p.setOptions(opts)
  669. if p.maxExprCnt == 0 {
  670. p.maxExprCnt = math.MaxUint64
  671. }
  672. return p
  673. }
  674. // setOptions applies the options to the parser.
  675. func (p *parser) setOptions(opts []Option) {
  676. for _, opt := range opts {
  677. opt(p)
  678. }
  679. }
  680. type resultTuple struct {
  681. v interface{}
  682. b bool
  683. end savepoint
  684. }
  685. const choiceNoMatch = -1
  686. // Stats stores some statistics, gathered during parsing
  687. type Stats struct {
  688. // ExprCnt counts the number of expressions processed during parsing
  689. // This value is compared to the maximum number of expressions allowed
  690. // (set by the MaxExpressions option).
  691. ExprCnt uint64
  692. // ChoiceAltCnt is used to count for each ordered choice expression,
  693. // which alternative is used how may times.
  694. // These numbers allow to optimize the order of the ordered choice expression
  695. // to increase the performance of the parser
  696. //
  697. // The outer key of ChoiceAltCnt is composed of the name of the rule as well
  698. // as the line and the column of the ordered choice.
  699. // The inner key of ChoiceAltCnt is the number (one-based) of the matching alternative.
  700. // For each alternative the number of matches are counted. If an ordered choice does not
  701. // match, a special counter is incremented. The name of this counter is set with
  702. // the parser option Statistics.
  703. // For an alternative to be included in ChoiceAltCnt, it has to match at least once.
  704. ChoiceAltCnt map[string]map[string]int
  705. }
  706. type parser struct {
  707. filename string
  708. pt savepoint
  709. cur current
  710. data []byte
  711. errs *errList
  712. depth int
  713. recover bool
  714. debug bool
  715. memoize bool
  716. // memoization table for the packrat algorithm:
  717. // map[offset in source] map[expression or rule] {value, match}
  718. memo map[int]map[interface{}]resultTuple
  719. // rules table, maps the rule identifier to the rule node
  720. rules map[string]*rule
  721. // variables stack, map of label to value
  722. vstack []map[string]interface{}
  723. // rule stack, allows identification of the current rule in errors
  724. rstack []*rule
  725. // parse fail
  726. maxFailPos position
  727. maxFailExpected []string
  728. maxFailInvertExpected bool
  729. // max number of expressions to be parsed
  730. maxExprCnt uint64
  731. // entrypoint for the parser
  732. entrypoint string
  733. allowInvalidUTF8 bool
  734. *Stats
  735. choiceNoMatch string
  736. // recovery expression stack, keeps track of the currently available recovery expression, these are traversed in reverse
  737. recoveryStack []map[string]interface{}
  738. }
  739. // push a variable set on the vstack.
  740. func (p *parser) pushV() {
  741. if cap(p.vstack) == len(p.vstack) {
  742. // create new empty slot in the stack
  743. p.vstack = append(p.vstack, nil)
  744. } else {
  745. // slice to 1 more
  746. p.vstack = p.vstack[:len(p.vstack)+1]
  747. }
  748. // get the last args set
  749. m := p.vstack[len(p.vstack)-1]
  750. if m != nil && len(m) == 0 {
  751. // empty map, all good
  752. return
  753. }
  754. m = make(map[string]interface{})
  755. p.vstack[len(p.vstack)-1] = m
  756. }
  757. // pop a variable set from the vstack.
  758. func (p *parser) popV() {
  759. // if the map is not empty, clear it
  760. m := p.vstack[len(p.vstack)-1]
  761. if len(m) > 0 {
  762. // GC that map
  763. p.vstack[len(p.vstack)-1] = nil
  764. }
  765. p.vstack = p.vstack[:len(p.vstack)-1]
  766. }
  767. // push a recovery expression with its labels to the recoveryStack
  768. func (p *parser) pushRecovery(labels []string, expr interface{}) {
  769. if cap(p.recoveryStack) == len(p.recoveryStack) {
  770. // create new empty slot in the stack
  771. p.recoveryStack = append(p.recoveryStack, nil)
  772. } else {
  773. // slice to 1 more
  774. p.recoveryStack = p.recoveryStack[:len(p.recoveryStack)+1]
  775. }
  776. m := make(map[string]interface{}, len(labels))
  777. for _, fl := range labels {
  778. m[fl] = expr
  779. }
  780. p.recoveryStack[len(p.recoveryStack)-1] = m
  781. }
  782. // pop a recovery expression from the recoveryStack
  783. func (p *parser) popRecovery() {
  784. // GC that map
  785. p.recoveryStack[len(p.recoveryStack)-1] = nil
  786. p.recoveryStack = p.recoveryStack[:len(p.recoveryStack)-1]
  787. }
  788. func (p *parser) print(prefix, s string) string {
  789. if !p.debug {
  790. return s
  791. }
  792. fmt.Printf("%s %d:%d:%d: %s [%#U]\n",
  793. prefix, p.pt.line, p.pt.col, p.pt.offset, s, p.pt.rn)
  794. return s
  795. }
  796. func (p *parser) in(s string) string {
  797. p.depth++
  798. return p.print(strings.Repeat(" ", p.depth)+">", s)
  799. }
  800. func (p *parser) out(s string) string {
  801. p.depth--
  802. return p.print(strings.Repeat(" ", p.depth)+"<", s)
  803. }
  804. func (p *parser) addErr(err error) {
  805. p.addErrAt(err, p.pt.position, []string{})
  806. }
  807. func (p *parser) addErrAt(err error, pos position, expected []string) {
  808. var buf bytes.Buffer
  809. if p.filename != "" {
  810. buf.WriteString(p.filename)
  811. }
  812. if buf.Len() > 0 {
  813. buf.WriteString(":")
  814. }
  815. buf.WriteString(fmt.Sprintf("%d:%d (%d)", pos.line, pos.col, pos.offset))
  816. if len(p.rstack) > 0 {
  817. if buf.Len() > 0 {
  818. buf.WriteString(": ")
  819. }
  820. rule := p.rstack[len(p.rstack)-1]
  821. if rule.displayName != "" {
  822. buf.WriteString("rule " + rule.displayName)
  823. } else {
  824. buf.WriteString("rule " + rule.name)
  825. }
  826. }
  827. pe := &parserError{Inner: err, pos: pos, prefix: buf.String(), expected: expected}
  828. p.errs.add(pe)
  829. }
  830. func (p *parser) failAt(fail bool, pos position, want string) {
  831. // process fail if parsing fails and not inverted or parsing succeeds and invert is set
  832. if fail == p.maxFailInvertExpected {
  833. if pos.offset < p.maxFailPos.offset {
  834. return
  835. }
  836. if pos.offset > p.maxFailPos.offset {
  837. p.maxFailPos = pos
  838. p.maxFailExpected = p.maxFailExpected[:0]
  839. }
  840. if p.maxFailInvertExpected {
  841. want = "!" + want
  842. }
  843. p.maxFailExpected = append(p.maxFailExpected, want)
  844. }
  845. }
  846. // read advances the parser to the next rune.
  847. func (p *parser) read() {
  848. p.pt.offset += p.pt.w
  849. rn, n := utf8.DecodeRune(p.data[p.pt.offset:])
  850. p.pt.rn = rn
  851. p.pt.w = n
  852. p.pt.col++
  853. if rn == '\n' {
  854. p.pt.line++
  855. p.pt.col = 0
  856. }
  857. if rn == utf8.RuneError && n == 1 { // see utf8.DecodeRune
  858. if !p.allowInvalidUTF8 {
  859. p.addErr(errInvalidEncoding)
  860. }
  861. }
  862. }
  863. // restore parser position to the savepoint pt.
  864. func (p *parser) restore(pt savepoint) {
  865. if p.debug {
  866. defer p.out(p.in("restore"))
  867. }
  868. if pt.offset == p.pt.offset {
  869. return
  870. }
  871. p.pt = pt
  872. }
  873. // Cloner is implemented by any value that has a Clone method, which returns a
  874. // copy of the value. This is mainly used for types which are not passed by
  875. // value (e.g map, slice, chan) or structs that contain such types.
  876. //
  877. // This is used in conjunction with the global state feature to create proper
  878. // copies of the state to allow the parser to properly restore the state in
  879. // the case of backtracking.
  880. type Cloner interface {
  881. Clone() interface{}
  882. }
  883. var statePool = &sync.Pool{
  884. New: func() interface{} { return make(storeDict) },
  885. }
  886. func (sd storeDict) Discard() {
  887. for k := range sd {
  888. delete(sd, k)
  889. }
  890. statePool.Put(sd)
  891. }
  892. // clone and return parser current state.
  893. func (p *parser) cloneState() storeDict {
  894. if p.debug {
  895. defer p.out(p.in("cloneState"))
  896. }
  897. state := statePool.Get().(storeDict)
  898. for k, v := range p.cur.state {
  899. if c, ok := v.(Cloner); ok {
  900. state[k] = c.Clone()
  901. } else {
  902. state[k] = v
  903. }
  904. }
  905. return state
  906. }
  907. // restore parser current state to the state storeDict.
  908. // every restoreState should applied only one time for every cloned state
  909. func (p *parser) restoreState(state storeDict) {
  910. if p.debug {
  911. defer p.out(p.in("restoreState"))
  912. }
  913. p.cur.state.Discard()
  914. p.cur.state = state
  915. }
  916. // get the slice of bytes from the savepoint start to the current position.
  917. func (p *parser) sliceFrom(start savepoint) []byte {
  918. return p.data[start.position.offset:p.pt.position.offset]
  919. }
  920. func (p *parser) getMemoized(node interface{}) (resultTuple, bool) {
  921. if len(p.memo) == 0 {
  922. return resultTuple{}, false
  923. }
  924. m := p.memo[p.pt.offset]
  925. if len(m) == 0 {
  926. return resultTuple{}, false
  927. }
  928. res, ok := m[node]
  929. return res, ok
  930. }
  931. func (p *parser) setMemoized(pt savepoint, node interface{}, tuple resultTuple) {
  932. if p.memo == nil {
  933. p.memo = make(map[int]map[interface{}]resultTuple)
  934. }
  935. m := p.memo[pt.offset]
  936. if m == nil {
  937. m = make(map[interface{}]resultTuple)
  938. p.memo[pt.offset] = m
  939. }
  940. m[node] = tuple
  941. }
  942. func (p *parser) buildRulesTable(g *grammar) {
  943. p.rules = make(map[string]*rule, len(g.rules))
  944. for _, r := range g.rules {
  945. p.rules[r.name] = r
  946. }
  947. }
  948. func (p *parser) parse(g *grammar) (val interface{}, err error) {
  949. if len(g.rules) == 0 {
  950. p.addErr(errNoRule)
  951. return nil, p.errs.err()
  952. }
  953. // TODO : not super critical but this could be generated
  954. p.buildRulesTable(g)
  955. if p.recover {
  956. // panic can be used in action code to stop parsing immediately
  957. // and return the panic as an error.
  958. defer func() {
  959. if e := recover(); e != nil {
  960. if p.debug {
  961. defer p.out(p.in("panic handler"))
  962. }
  963. val = nil
  964. switch e := e.(type) {
  965. case error:
  966. p.addErr(e)
  967. default:
  968. p.addErr(fmt.Errorf("%v", e))
  969. }
  970. err = p.errs.err()
  971. }
  972. }()
  973. }
  974. startRule, ok := p.rules[p.entrypoint]
  975. if !ok {
  976. p.addErr(errInvalidEntrypoint)
  977. return nil, p.errs.err()
  978. }
  979. p.read() // advance to first rune
  980. val, ok = p.parseRule(startRule)
  981. if !ok {
  982. if len(*p.errs) == 0 {
  983. // If parsing fails, but no errors have been recorded, the expected values
  984. // for the farthest parser position are returned as error.
  985. maxFailExpectedMap := make(map[string]struct{}, len(p.maxFailExpected))
  986. for _, v := range p.maxFailExpected {
  987. maxFailExpectedMap[v] = struct{}{}
  988. }
  989. expected := make([]string, 0, len(maxFailExpectedMap))
  990. eof := false
  991. if _, ok := maxFailExpectedMap["!."]; ok {
  992. delete(maxFailExpectedMap, "!.")
  993. eof = true
  994. }
  995. for k := range maxFailExpectedMap {
  996. expected = append(expected, k)
  997. }
  998. sort.Strings(expected)
  999. if eof {
  1000. expected = append(expected, "EOF")
  1001. }
  1002. p.addErrAt(errors.New("no match found, expected: "+listJoin(expected, ", ", "or")), p.maxFailPos, expected)
  1003. }
  1004. return nil, p.errs.err()
  1005. }
  1006. return val, p.errs.err()
  1007. }
  1008. func listJoin(list []string, sep string, lastSep string) string {
  1009. switch len(list) {
  1010. case 0:
  1011. return ""
  1012. case 1:
  1013. return list[0]
  1014. default:
  1015. return strings.Join(list[:len(list)-1], sep) + " " + lastSep + " " + list[len(list)-1]
  1016. }
  1017. }
  1018. func (p *parser) parseRule(rule *rule) (interface{}, bool) {
  1019. if p.debug {
  1020. defer p.out(p.in("parseRule " + rule.name))
  1021. }
  1022. if p.memoize {
  1023. res, ok := p.getMemoized(rule)
  1024. if ok {
  1025. p.restore(res.end)
  1026. return res.v, res.b
  1027. }
  1028. }
  1029. start := p.pt
  1030. p.rstack = append(p.rstack, rule)
  1031. p.pushV()
  1032. val, ok := p.parseExpr(rule.expr)
  1033. p.popV()
  1034. p.rstack = p.rstack[:len(p.rstack)-1]
  1035. if ok && p.debug {
  1036. p.print(strings.Repeat(" ", p.depth)+"MATCH", string(p.sliceFrom(start)))
  1037. }
  1038. if p.memoize {
  1039. p.setMemoized(start, rule, resultTuple{val, ok, p.pt})
  1040. }
  1041. return val, ok
  1042. }
  1043. func (p *parser) parseExpr(expr interface{}) (interface{}, bool) {
  1044. var pt savepoint
  1045. if p.memoize {
  1046. res, ok := p.getMemoized(expr)
  1047. if ok {
  1048. p.restore(res.end)
  1049. return res.v, res.b
  1050. }
  1051. pt = p.pt
  1052. }
  1053. p.ExprCnt++
  1054. if p.ExprCnt > p.maxExprCnt {
  1055. panic(errMaxExprCnt)
  1056. }
  1057. var val interface{}
  1058. var ok bool
  1059. switch expr := expr.(type) {
  1060. case *actionExpr:
  1061. val, ok = p.parseActionExpr(expr)
  1062. case *andCodeExpr:
  1063. val, ok = p.parseAndCodeExpr(expr)
  1064. case *andExpr:
  1065. val, ok = p.parseAndExpr(expr)
  1066. case *anyMatcher:
  1067. val, ok = p.parseAnyMatcher(expr)
  1068. case *charClassMatcher:
  1069. val, ok = p.parseCharClassMatcher(expr)
  1070. case *choiceExpr:
  1071. val, ok = p.parseChoiceExpr(expr)
  1072. case *labeledExpr:
  1073. val, ok = p.parseLabeledExpr(expr)
  1074. case *litMatcher:
  1075. val, ok = p.parseLitMatcher(expr)
  1076. case *notCodeExpr:
  1077. val, ok = p.parseNotCodeExpr(expr)
  1078. case *notExpr:
  1079. val, ok = p.parseNotExpr(expr)
  1080. case *oneOrMoreExpr:
  1081. val, ok = p.parseOneOrMoreExpr(expr)
  1082. case *recoveryExpr:
  1083. val, ok = p.parseRecoveryExpr(expr)
  1084. case *ruleRefExpr:
  1085. val, ok = p.parseRuleRefExpr(expr)
  1086. case *seqExpr:
  1087. val, ok = p.parseSeqExpr(expr)
  1088. case *stateCodeExpr:
  1089. val, ok = p.parseStateCodeExpr(expr)
  1090. case *throwExpr:
  1091. val, ok = p.parseThrowExpr(expr)
  1092. case *zeroOrMoreExpr:
  1093. val, ok = p.parseZeroOrMoreExpr(expr)
  1094. case *zeroOrOneExpr:
  1095. val, ok = p.parseZeroOrOneExpr(expr)
  1096. default:
  1097. panic(fmt.Sprintf("unknown expression type %T", expr))
  1098. }
  1099. if p.memoize {
  1100. p.setMemoized(pt, expr, resultTuple{val, ok, p.pt})
  1101. }
  1102. return val, ok
  1103. }
  1104. func (p *parser) parseActionExpr(act *actionExpr) (interface{}, bool) {
  1105. if p.debug {
  1106. defer p.out(p.in("parseActionExpr"))
  1107. }
  1108. start := p.pt
  1109. val, ok := p.parseExpr(act.expr)
  1110. if ok {
  1111. p.cur.pos = start.position
  1112. p.cur.text = p.sliceFrom(start)
  1113. state := p.cloneState()
  1114. actVal, err := act.run(p)
  1115. if err != nil {
  1116. p.addErrAt(err, start.position, []string{})
  1117. }
  1118. p.restoreState(state)
  1119. val = actVal
  1120. }
  1121. if ok && p.debug {
  1122. p.print(strings.Repeat(" ", p.depth)+"MATCH", string(p.sliceFrom(start)))
  1123. }
  1124. return val, ok
  1125. }
  1126. func (p *parser) parseAndCodeExpr(and *andCodeExpr) (interface{}, bool) {
  1127. if p.debug {
  1128. defer p.out(p.in("parseAndCodeExpr"))
  1129. }
  1130. state := p.cloneState()
  1131. ok, err := and.run(p)
  1132. if err != nil {
  1133. p.addErr(err)
  1134. }
  1135. p.restoreState(state)
  1136. return nil, ok
  1137. }
  1138. func (p *parser) parseAndExpr(and *andExpr) (interface{}, bool) {
  1139. if p.debug {
  1140. defer p.out(p.in("parseAndExpr"))
  1141. }
  1142. pt := p.pt
  1143. state := p.cloneState()
  1144. p.pushV()
  1145. _, ok := p.parseExpr(and.expr)
  1146. p.popV()
  1147. p.restoreState(state)
  1148. p.restore(pt)
  1149. return nil, ok
  1150. }
  1151. func (p *parser) parseAnyMatcher(any *anyMatcher) (interface{}, bool) {
  1152. if p.debug {
  1153. defer p.out(p.in("parseAnyMatcher"))
  1154. }
  1155. if p.pt.rn == utf8.RuneError && p.pt.w == 0 {
  1156. // EOF - see utf8.DecodeRune
  1157. p.failAt(false, p.pt.position, ".")
  1158. return nil, false
  1159. }
  1160. start := p.pt
  1161. p.read()
  1162. p.failAt(true, start.position, ".")
  1163. return p.sliceFrom(start), true
  1164. }
  1165. func (p *parser) parseCharClassMatcher(chr *charClassMatcher) (interface{}, bool) {
  1166. if p.debug {
  1167. defer p.out(p.in("parseCharClassMatcher"))
  1168. }
  1169. cur := p.pt.rn
  1170. start := p.pt
  1171. // can't match EOF
  1172. if cur == utf8.RuneError && p.pt.w == 0 { // see utf8.DecodeRune
  1173. p.failAt(false, start.position, chr.val)
  1174. return nil, false
  1175. }
  1176. if chr.ignoreCase {
  1177. cur = unicode.ToLower(cur)
  1178. }
  1179. // try to match in the list of available chars
  1180. for _, rn := range chr.chars {
  1181. if rn == cur {
  1182. if chr.inverted {
  1183. p.failAt(false, start.position, chr.val)
  1184. return nil, false
  1185. }
  1186. p.read()
  1187. p.failAt(true, start.position, chr.val)
  1188. return p.sliceFrom(start), true
  1189. }
  1190. }
  1191. // try to match in the list of ranges
  1192. for i := 0; i < len(chr.ranges); i += 2 {
  1193. if cur >= chr.ranges[i] && cur <= chr.ranges[i+1] {
  1194. if chr.inverted {
  1195. p.failAt(false, start.position, chr.val)
  1196. return nil, false
  1197. }
  1198. p.read()
  1199. p.failAt(true, start.position, chr.val)
  1200. return p.sliceFrom(start), true
  1201. }
  1202. }
  1203. // try to match in the list of Unicode classes
  1204. for _, cl := range chr.classes {
  1205. if unicode.Is(cl, cur) {
  1206. if chr.inverted {
  1207. p.failAt(false, start.position, chr.val)
  1208. return nil, false
  1209. }
  1210. p.read()
  1211. p.failAt(true, start.position, chr.val)
  1212. return p.sliceFrom(start), true
  1213. }
  1214. }
  1215. if chr.inverted {
  1216. p.read()
  1217. p.failAt(true, start.position, chr.val)
  1218. return p.sliceFrom(start), true
  1219. }
  1220. p.failAt(false, start.position, chr.val)
  1221. return nil, false
  1222. }
  1223. func (p *parser) incChoiceAltCnt(ch *choiceExpr, altI int) {
  1224. choiceIdent := fmt.Sprintf("%s %d:%d", p.rstack[len(p.rstack)-1].name, ch.pos.line, ch.pos.col)
  1225. m := p.ChoiceAltCnt[choiceIdent]
  1226. if m == nil {
  1227. m = make(map[string]int)
  1228. p.ChoiceAltCnt[choiceIdent] = m
  1229. }
  1230. // We increment altI by 1, so the keys do not start at 0
  1231. alt := strconv.Itoa(altI + 1)
  1232. if altI == choiceNoMatch {
  1233. alt = p.choiceNoMatch
  1234. }
  1235. m[alt]++
  1236. }
  1237. func (p *parser) parseChoiceExpr(ch *choiceExpr) (interface{}, bool) {
  1238. if p.debug {
  1239. defer p.out(p.in("parseChoiceExpr"))
  1240. }
  1241. for altI, alt := range ch.alternatives {
  1242. // dummy assignment to prevent compile error if optimized
  1243. _ = altI
  1244. state := p.cloneState()
  1245. p.pushV()
  1246. val, ok := p.parseExpr(alt)
  1247. p.popV()
  1248. if ok {
  1249. p.incChoiceAltCnt(ch, altI)
  1250. return val, ok
  1251. }
  1252. p.restoreState(state)
  1253. }
  1254. p.incChoiceAltCnt(ch, choiceNoMatch)
  1255. return nil, false
  1256. }
  1257. func (p *parser) parseLabeledExpr(lab *labeledExpr) (interface{}, bool) {
  1258. if p.debug {
  1259. defer p.out(p.in("parseLabeledExpr"))
  1260. }
  1261. p.pushV()
  1262. val, ok := p.parseExpr(lab.expr)
  1263. p.popV()
  1264. if ok && lab.label != "" {
  1265. m := p.vstack[len(p.vstack)-1]
  1266. m[lab.label] = val
  1267. }
  1268. return val, ok
  1269. }
  1270. func (p *parser) parseLitMatcher(lit *litMatcher) (interface{}, bool) {
  1271. if p.debug {
  1272. defer p.out(p.in("parseLitMatcher"))
  1273. }
  1274. start := p.pt
  1275. for _, want := range lit.val {
  1276. cur := p.pt.rn
  1277. if lit.ignoreCase {
  1278. cur = unicode.ToLower(cur)
  1279. }
  1280. if cur != want {
  1281. p.failAt(false, start.position, lit.want)
  1282. p.restore(start)
  1283. return nil, false
  1284. }
  1285. p.read()
  1286. }
  1287. p.failAt(true, start.position, lit.want)
  1288. return p.sliceFrom(start), true
  1289. }
  1290. func (p *parser) parseNotCodeExpr(not *notCodeExpr) (interface{}, bool) {
  1291. if p.debug {
  1292. defer p.out(p.in("parseNotCodeExpr"))
  1293. }
  1294. state := p.cloneState()
  1295. ok, err := not.run(p)
  1296. if err != nil {
  1297. p.addErr(err)
  1298. }
  1299. p.restoreState(state)
  1300. return nil, !ok
  1301. }
  1302. func (p *parser) parseNotExpr(not *notExpr) (interface{}, bool) {
  1303. if p.debug {
  1304. defer p.out(p.in("parseNotExpr"))
  1305. }
  1306. pt := p.pt
  1307. state := p.cloneState()
  1308. p.pushV()
  1309. p.maxFailInvertExpected = !p.maxFailInvertExpected
  1310. _, ok := p.parseExpr(not.expr)
  1311. p.maxFailInvertExpected = !p.maxFailInvertExpected
  1312. p.popV()
  1313. p.restoreState(state)
  1314. p.restore(pt)
  1315. return nil, !ok
  1316. }
  1317. func (p *parser) parseOneOrMoreExpr(expr *oneOrMoreExpr) (interface{}, bool) {
  1318. if p.debug {
  1319. defer p.out(p.in("parseOneOrMoreExpr"))
  1320. }
  1321. var vals []interface{}
  1322. for {
  1323. p.pushV()
  1324. val, ok := p.parseExpr(expr.expr)
  1325. p.popV()
  1326. if !ok {
  1327. if len(vals) == 0 {
  1328. // did not match once, no match
  1329. return nil, false
  1330. }
  1331. return vals, true
  1332. }
  1333. vals = append(vals, val)
  1334. }
  1335. }
  1336. func (p *parser) parseRecoveryExpr(recover *recoveryExpr) (interface{}, bool) {
  1337. if p.debug {
  1338. defer p.out(p.in("parseRecoveryExpr (" + strings.Join(recover.failureLabel, ",") + ")"))
  1339. }
  1340. p.pushRecovery(recover.failureLabel, recover.recoverExpr)
  1341. val, ok := p.parseExpr(recover.expr)
  1342. p.popRecovery()
  1343. return val, ok
  1344. }
  1345. func (p *parser) parseRuleRefExpr(ref *ruleRefExpr) (interface{}, bool) {
  1346. if p.debug {
  1347. defer p.out(p.in("parseRuleRefExpr " + ref.name))
  1348. }
  1349. if ref.name == "" {
  1350. panic(fmt.Sprintf("%s: invalid rule: missing name", ref.pos))
  1351. }
  1352. rule := p.rules[ref.name]
  1353. if rule == nil {
  1354. p.addErr(fmt.Errorf("undefined rule: %s", ref.name))
  1355. return nil, false
  1356. }
  1357. return p.parseRule(rule)
  1358. }
  1359. func (p *parser) parseSeqExpr(seq *seqExpr) (interface{}, bool) {
  1360. if p.debug {
  1361. defer p.out(p.in("parseSeqExpr"))
  1362. }
  1363. vals := make([]interface{}, 0, len(seq.exprs))
  1364. pt := p.pt
  1365. state := p.cloneState()
  1366. for _, expr := range seq.exprs {
  1367. val, ok := p.parseExpr(expr)
  1368. if !ok {
  1369. p.restoreState(state)
  1370. p.restore(pt)
  1371. return nil, false
  1372. }
  1373. vals = append(vals, val)
  1374. }
  1375. return vals, true
  1376. }
  1377. func (p *parser) parseStateCodeExpr(state *stateCodeExpr) (interface{}, bool) {
  1378. if p.debug {
  1379. defer p.out(p.in("parseStateCodeExpr"))
  1380. }
  1381. err := state.run(p)
  1382. if err != nil {
  1383. p.addErr(err)
  1384. }
  1385. return nil, true
  1386. }
  1387. func (p *parser) parseThrowExpr(expr *throwExpr) (interface{}, bool) {
  1388. if p.debug {
  1389. defer p.out(p.in("parseThrowExpr"))
  1390. }
  1391. for i := len(p.recoveryStack) - 1; i >= 0; i-- {
  1392. if recoverExpr, ok := p.recoveryStack[i][expr.label]; ok {
  1393. if val, ok := p.parseExpr(recoverExpr); ok {
  1394. return val, ok
  1395. }
  1396. }
  1397. }
  1398. return nil, false
  1399. }
  1400. func (p *parser) parseZeroOrMoreExpr(expr *zeroOrMoreExpr) (interface{}, bool) {
  1401. if p.debug {
  1402. defer p.out(p.in("parseZeroOrMoreExpr"))
  1403. }
  1404. var vals []interface{}
  1405. for {
  1406. p.pushV()
  1407. val, ok := p.parseExpr(expr.expr)
  1408. p.popV()
  1409. if !ok {
  1410. return vals, true
  1411. }
  1412. vals = append(vals, val)
  1413. }
  1414. }
  1415. func (p *parser) parseZeroOrOneExpr(expr *zeroOrOneExpr) (interface{}, bool) {
  1416. if p.debug {
  1417. defer p.out(p.in("parseZeroOrOneExpr"))
  1418. }
  1419. p.pushV()
  1420. val, _ := p.parseExpr(expr.expr)
  1421. p.popV()
  1422. // whether it matched or not, consider it a match
  1423. return val, true
  1424. }