ll1_parser.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * ll1_parser.go: Parser for the Grammar grammar.
  3. * Generated by the ll1 tool from ll1.ll1 at 2020-08-28 18:00:14.138349709 +0200 CEST m=+0.001367333.
  4. * Based on template: ll1.parser.go.lined.tpl
  5. * Uses a scanner
  6. *
  7. * Available definition keys at template expansion:
  8. * [Grammar Import InName LexerType OutName Package Parser Prefix Templates]
  9. *
  10. * DO NOT EDIT.
  11. */
  12. package main
  13. import "text/scanner"
  14. import "unicode"
  15. import "io"
  16. import "os"
  17. import "fmt"
  18. // Ll1Value is the lexical value of a lexer token.
  19. // This is based on strings as a default.
  20. //line ll1.parser.go.tpl:43
  21. type Ll1Value = string
  22. // Ll1Position is a position within a source file. Since the lexer is based on
  23. // text/scanner, we use that package's Position.
  24. //line ll1.parser.go.tpl:51
  25. type Ll1Position = scanner.Position
  26. // Ll1TokenKind is the kind or type of a token.
  27. // This has rune as the underlying type so one-character tokens can be easily
  28. // supported. EOF will be 65535 (I.e, -1 cast to rune). Non-character token
  29. // kinds will start from 65533 down (i.e -3, -4, -5, etc).
  30. //line ll1.parser.go.tpl:69
  31. type Ll1TokenKind rune
  32. // NoLl1TokenKind means "no token kind" i.e. no token.
  33. //line ll1.parser.go.tpl:74
  34. const NoLl1TokenKind Ll1TokenKind = Ll1TokenKind(0)
  35. // Ll1TokenKindEOF means the end of the input.
  36. //line ll1.parser.go.tpl:77
  37. const Ll1TokenKindEOF Ll1TokenKind = Ll1TokenKind(-1)
  38. // Ll1TokenKindError means a parsing or lexing error was encountered.
  39. //line ll1.parser.go.tpl:80
  40. const Ll1TokenKindError Ll1TokenKind = Ll1TokenKind(-2)
  41. // Convert token kind to a string representation
  42. //line ll1.parser.go.tpl:86
  43. func (tk Ll1TokenKind) String() string {
  44. return scanner.TokenString(rune(tk))
  45. }
  46. // Ll1Token is the result of a single lexical analysis step by the lexer.
  47. //line ll1.parser.go.tpl:109
  48. type Ll1Token struct {
  49. Ll1Position // Position in the source where the token was found.
  50. Ll1TokenKind // Type of the token
  51. Ll1Value // Value of the token
  52. }
  53. // MakeLl1Token makes a token with the given position, type and value.
  54. //line ll1.parser.go.tpl:118
  55. func MakeLl1Token(pos Ll1Position, typ Ll1TokenKind, val Ll1Value) Ll1Token {
  56. return Ll1Token{ pos, typ, val}
  57. }
  58. // Ll1Lexer performs the lexical analysis of the input.
  59. //line ll1.parser.go.tpl:124
  60. type Ll1Lexer struct {
  61. // Embed scanner.Scanner
  62. scanner.Scanner
  63. Filename string
  64. }
  65. // NewLl1LexerFromReader creates a new lexer for the given parser and input.
  66. //line ll1.parser.go.tpl:133
  67. func NewLl1LexerFromReader(parser *Ll1Parser, reader io.Reader, filename string) *Ll1Lexer {
  68. lexer := &Ll1Lexer{}
  69. lexer.Filename = filename
  70. lexer.Scanner.Init(reader)
  71. lexer.Scanner.Mode = scanner.GoTokens
  72. lexer.Scanner.Error = func (s *scanner.Scanner, msg string) {
  73. parser.Panicf("%s: scanner error: %s, %s", s.Position, s.TokenText(), msg)
  74. }
  75. // XXX: needs to be generated from the identifier rule in the syntax!
  76. lexer.Scanner.IsIdentRune = func(ch rune, i int) bool {
  77. if i == 0 {
  78. return unicode.IsLetter(ch)
  79. }
  80. return unicode.IsLetter(ch) ||
  81. unicode.IsNumber(ch) ||
  82. ch == '_' ||
  83. ch == '-'
  84. }
  85. return lexer
  86. }
  87. //line ll1.parser.go.tpl:155
  88. func (lex *Ll1Lexer) Lex() Ll1Token {
  89. scanned := lex.Scanner.Scan()
  90. pos := lex.Scanner.Position
  91. pos.Filename = lex.Filename
  92. value := lex.Scanner.TokenText()
  93. // Get rid of the quotes
  94. if scanned == scanner.Char ||
  95. scanned == scanner.String ||
  96. scanned == scanner.RawString {
  97. value = value[1:len(value) - 1]
  98. }
  99. token := Ll1Token {
  100. Ll1TokenKind: Ll1TokenKind(scanned),
  101. Ll1Value: value,
  102. Ll1Position: pos,
  103. }
  104. return token
  105. }
  106. // Ll1Parser parses the input and returns a parse tree,
  107. // based on the rules in ll1.ll1
  108. //line ll1.parser.go.tpl:188
  109. type Ll1Parser struct {
  110. reader io.Reader
  111. lexer *Ll1Lexer
  112. current Ll1Token
  113. Errors []Ll1ParserError
  114. Filename string
  115. Debug io.Writer
  116. }
  117. //line ll1.parser.go.tpl:198
  118. func NewLl1ParserFromReader(reader io.Reader, filename string, debug bool) *Ll1Parser {
  119. parser := &Ll1Parser{}
  120. parser.lexer = NewLl1LexerFromReader(parser, reader, filename)
  121. parser.Filename = filename
  122. parser.current.Ll1TokenKind = NoLl1TokenKind
  123. parser.Debug = nil
  124. if debug {
  125. parser.Debug = os.Stderr
  126. }
  127. return parser
  128. }
  129. // Advances the parser. Returns the current token /after/ advancing.
  130. //line ll1.parser.go.tpl:214
  131. func (p *Ll1Parser) Advance() Ll1Token {
  132. token := p.lexer.Lex()
  133. p.Debugf("Lexed token: %v", token)
  134. p.current = token
  135. return token
  136. }
  137. // Ll1ParserError is an error encountered during parsing or lexing.
  138. // The parser may panic with this type on errors that would prevent the parser
  139. // from making progress.
  140. //line ll1.parser.go.tpl:225
  141. type Ll1ParserError struct {
  142. *Ll1Parser // Parser that had the error.
  143. *Ll1Token // Token at which the error was found
  144. Chain error // underlying error
  145. }
  146. //line ll1.parser.go.tpl:232
  147. func (pe Ll1ParserError) Error() string {
  148. // XXX will need to be improved
  149. return pe.Chain.Error()
  150. }
  151. //line ll1.parser.go.tpl:238
  152. func (parser *Ll1Parser) Errorf(message string, args ...interface{}) Ll1ParserError {
  153. err := fmt.Errorf(message, args...)
  154. pe := Ll1ParserError {
  155. Ll1Parser: parser,
  156. Ll1Token: &parser.current,
  157. Chain: err,
  158. }
  159. parser.Errors = append(parser.Errors, pe)
  160. return pe
  161. }
  162. //line ll1.parser.go.tpl:250
  163. func (parser *Ll1Parser) Panicf(message string, args ...interface{}) {
  164. pe := parser.Errorf(message, args...)
  165. panic(pe)
  166. }
  167. //line ll1.parser.go.tpl:257
  168. func (p *Ll1Parser) Debugf(message string, args ...interface{}) {
  169. if p.Debug != nil {
  170. fmt.Fprintf(p.Debug, message, args)
  171. }
  172. }
  173. /* Looks at the current token and advances the lexer if the token is of any of
  174. the token kinds given in kinds. In this case it will return the accepted
  175. token and advance the parser. Otherwise, it will call parser.Panicf.*/
  176. //line ll1.parser.go.tpl:267
  177. func (parser *Ll1Parser) Require(kinds ...Ll1TokenKind) Ll1Token {
  178. parser.Debugf("Require: %v\n", kinds)
  179. if parser.current.Ll1TokenKind == Ll1TokenKind(0) {
  180. parser.Advance()
  181. }
  182. expected := ""
  183. sep := ""
  184. for _, kind := range kinds {
  185. if kind == parser.current.Ll1TokenKind {
  186. accepted := parser.current
  187. parser.Advance()
  188. return accepted
  189. }
  190. expected = fmt.Sprintf("%s%s%s", expected, sep, kind.String())
  191. }
  192. parser.Panicf("error: expected one of the following: %s", expected)
  193. return Ll1Token{}
  194. }
  195. //line ll1.parser.go.tpl:288
  196. func (parser Ll1Parser) NextIs(kinds ...Ll1TokenKind) bool {
  197. parser.Debugf("NextIs: %v\n", kinds)
  198. if (parser.current.Ll1TokenKind == 0) {
  199. parser.Advance()
  200. }
  201. for _, kind := range kinds {
  202. if kind == parser.current.Ll1TokenKind {
  203. return true
  204. }
  205. }
  206. return false
  207. }
  208. //line ll1.parser.go.tpl:324
  209. type Ll1Grammar struct {
  210. }
  211. //line ll1.parser.go.tpl:328
  212. func ( *Ll1Parser) ParseLl1Grammar() (Ll1Grammar, error) {
  213. result := Ll1Grammar {}
  214. return result, nil
  215. }
  216. //line ll1.parser.go.tpl:324
  217. type Ll1Rules struct {
  218. }
  219. //line ll1.parser.go.tpl:328
  220. func ( *Ll1Parser) ParseLl1Rules() (Ll1Rules, error) {
  221. result := Ll1Rules {}
  222. return result, nil
  223. }
  224. //line ll1.parser.go.tpl:324
  225. type Ll1OptRules struct {
  226. }
  227. //line ll1.parser.go.tpl:328
  228. func ( *Ll1Parser) ParseLl1OptRules() (Ll1OptRules, error) {
  229. result := Ll1OptRules {}
  230. return result, nil
  231. }
  232. //line ll1.parser.go.tpl:324
  233. type Ll1Rule struct {
  234. }
  235. //line ll1.parser.go.tpl:328
  236. func ( *Ll1Parser) ParseLl1Rule() (Ll1Rule, error) {
  237. result := Ll1Rule {}
  238. return result, nil
  239. }
  240. //line ll1.parser.go.tpl:324
  241. type Ll1Name struct {
  242. }
  243. //line ll1.parser.go.tpl:328
  244. func ( *Ll1Parser) ParseLl1Name() (Ll1Name, error) {
  245. result := Ll1Name {}
  246. return result, nil
  247. }
  248. //line ll1.parser.go.tpl:324
  249. type Ll1Template struct {
  250. }
  251. //line ll1.parser.go.tpl:328
  252. func ( *Ll1Parser) ParseLl1Template() (Ll1Template, error) {
  253. result := Ll1Template {}
  254. return result, nil
  255. }
  256. //line ll1.parser.go.tpl:324
  257. type Ll1Definition struct {
  258. }
  259. //line ll1.parser.go.tpl:328
  260. func ( *Ll1Parser) ParseLl1Definition() (Ll1Definition, error) {
  261. result := Ll1Definition {}
  262. return result, nil
  263. }
  264. //line ll1.parser.go.tpl:324
  265. type Ll1Alternates struct {
  266. }
  267. //line ll1.parser.go.tpl:328
  268. func ( *Ll1Parser) ParseLl1Alternates() (Ll1Alternates, error) {
  269. result := Ll1Alternates {}
  270. return result, nil
  271. }
  272. //line ll1.parser.go.tpl:324
  273. type Ll1OptSequences struct {
  274. }
  275. //line ll1.parser.go.tpl:328
  276. func ( *Ll1Parser) ParseLl1OptSequences() (Ll1OptSequences, error) {
  277. result := Ll1OptSequences {}
  278. return result, nil
  279. }
  280. //line ll1.parser.go.tpl:324
  281. type Ll1Sequence struct {
  282. }
  283. //line ll1.parser.go.tpl:328
  284. func ( *Ll1Parser) ParseLl1Sequence() (Ll1Sequence, error) {
  285. result := Ll1Sequence {}
  286. return result, nil
  287. }
  288. //line ll1.parser.go.tpl:324
  289. type Ll1OptElements struct {
  290. }
  291. //line ll1.parser.go.tpl:328
  292. func ( *Ll1Parser) ParseLl1OptElements() (Ll1OptElements, error) {
  293. result := Ll1OptElements {}
  294. return result, nil
  295. }
  296. //line ll1.parser.go.tpl:324
  297. type Ll1Element struct {
  298. }
  299. //line ll1.parser.go.tpl:328
  300. func ( *Ll1Parser) ParseLl1Element() (Ll1Element, error) {
  301. result := Ll1Element {}
  302. return result, nil
  303. }
  304. //line ll1.parser.go.tpl:324
  305. type Ll1Parenthesis struct {
  306. }
  307. //line ll1.parser.go.tpl:328
  308. func ( *Ll1Parser) ParseLl1Parenthesis() (Ll1Parenthesis, error) {
  309. result := Ll1Parenthesis {}
  310. return result, nil
  311. }
  312. //line ll1.parser.go.tpl:313
  313. const Ll1TokenKinddot Ll1TokenKind = Ll1TokenKind(-2)
  314. //line ll1.parser.go.tpl:316
  315. func ( *Ll1Lexer) LexLl1TokenKinddot() (Ll1TokenKind, error) {
  316. result := Ll1TokenKinddot
  317. return result, nil
  318. }
  319. //line ll1.parser.go.tpl:313
  320. const Ll1TokenKindor Ll1TokenKind = Ll1TokenKind(-3)
  321. //line ll1.parser.go.tpl:316
  322. func ( *Ll1Lexer) LexLl1TokenKindor() (Ll1TokenKind, error) {
  323. result := Ll1TokenKindor
  324. return result, nil
  325. }
  326. //line ll1.parser.go.tpl:313
  327. const Ll1TokenKindidentifier Ll1TokenKind = Ll1TokenKind(-4)
  328. //line ll1.parser.go.tpl:316
  329. func ( *Ll1Lexer) LexLl1TokenKindidentifier() (Ll1TokenKind, error) {
  330. result := Ll1TokenKindidentifier
  331. return result, nil
  332. }
  333. //line ll1.parser.go.tpl:313
  334. const Ll1TokenKindruleName Ll1TokenKind = Ll1TokenKind(-5)
  335. //line ll1.parser.go.tpl:316
  336. func ( *Ll1Lexer) LexLl1TokenKindruleName() (Ll1TokenKind, error) {
  337. result := Ll1TokenKindruleName
  338. return result, nil
  339. }
  340. //line ll1.parser.go.tpl:313
  341. const Ll1TokenKindterminalName Ll1TokenKind = Ll1TokenKind(-6)
  342. //line ll1.parser.go.tpl:316
  343. func ( *Ll1Lexer) LexLl1TokenKindterminalName() (Ll1TokenKind, error) {
  344. result := Ll1TokenKindterminalName
  345. return result, nil
  346. }
  347. //line ll1.parser.go.tpl:313
  348. const Ll1TokenKindepsilon Ll1TokenKind = Ll1TokenKind(-7)
  349. //line ll1.parser.go.tpl:316
  350. func ( *Ll1Lexer) LexLl1TokenKindepsilon() (Ll1TokenKind, error) {
  351. result := Ll1TokenKindepsilon
  352. return result, nil
  353. }
  354. //line ll1.parser.go.tpl:313
  355. const Ll1TokenKindarrow Ll1TokenKind = Ll1TokenKind(-8)
  356. //line ll1.parser.go.tpl:316
  357. func ( *Ll1Lexer) LexLl1TokenKindarrow() (Ll1TokenKind, error) {
  358. result := Ll1TokenKindarrow
  359. return result, nil
  360. }
  361. //line ll1.parser.go.tpl:313
  362. const Ll1TokenKindliteral Ll1TokenKind = Ll1TokenKind(-9)
  363. //line ll1.parser.go.tpl:316
  364. func ( *Ll1Lexer) LexLl1TokenKindliteral() (Ll1TokenKind, error) {
  365. result := Ll1TokenKindliteral
  366. return result, nil
  367. }
  368. //line ll1.parser.go.tpl:313
  369. const Ll1TokenKindstringLiteral Ll1TokenKind = Ll1TokenKind(-10)
  370. //line ll1.parser.go.tpl:316
  371. func ( *Ll1Lexer) LexLl1TokenKindstringLiteral() (Ll1TokenKind, error) {
  372. result := Ll1TokenKindstringLiteral
  373. return result, nil
  374. }
  375. //line ll1.parser.go.tpl:313
  376. const Ll1TokenKindcharLiteral Ll1TokenKind = Ll1TokenKind(-11)
  377. //line ll1.parser.go.tpl:316
  378. func ( *Ll1Lexer) LexLl1TokenKindcharLiteral() (Ll1TokenKind, error) {
  379. result := Ll1TokenKindcharLiteral
  380. return result, nil
  381. }
  382. //line ll1.parser.go.tpl:313
  383. const Ll1TokenKindrawString Ll1TokenKind = Ll1TokenKind(-12)
  384. //line ll1.parser.go.tpl:316
  385. func ( *Ll1Lexer) LexLl1TokenKindrawString() (Ll1TokenKind, error) {
  386. result := Ll1TokenKindrawString
  387. return result, nil
  388. }
  389. //line ll1.parser.go.tpl:313
  390. const Ll1TokenKindwhiteSpace Ll1TokenKind = Ll1TokenKind(-13)
  391. //line ll1.parser.go.tpl:316
  392. func ( *Ll1Lexer) LexLl1TokenKindwhiteSpace() (Ll1TokenKind, error) {
  393. result := Ll1TokenKindwhiteSpace
  394. return result, nil
  395. }
  396. //line ll1.parser.go.tpl:313
  397. const Ll1TokenKindlineComment Ll1TokenKind = Ll1TokenKind(-14)
  398. //line ll1.parser.go.tpl:316
  399. func ( *Ll1Lexer) LexLl1TokenKindlineComment() (Ll1TokenKind, error) {
  400. result := Ll1TokenKindlineComment
  401. return result, nil
  402. }
  403. // Expanded from template of rule handCoded
  404. // Not implemented
  405. //line ll1.parser.go.tpl:313
  406. const Ll1TokenKindhandCoded Ll1TokenKind = Ll1TokenKind(-15)
  407. //line ll1.parser.go.tpl:316
  408. func ( *Ll1Lexer) LexLl1TokenKindhandCoded() (Ll1TokenKind, error) {
  409. result := Ll1TokenKindhandCoded
  410. return result, nil
  411. }