ll1.parser.go.tpl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. {{- /* This template generates a recursive descent parser based on the */ -}}
  2. {{- /* information about the LL(1) grammar processed by the ll1 tool. */ -}}
  3. /*
  4. * {{.OutName}}: Parser for the {{.Grammar.Top.Name}} grammar.
  5. * Generated by the ll1 tool from {{.InName}} at {{Now}}.
  6. * Based on template: {{.Templates}}
  7. * Uses a scanner
  8. *
  9. * Available definition keys at template expansion:
  10. * {{.Definitions}}
  11. *
  12. * DO NOT EDIT.
  13. */
  14. package {{ .Package }}
  15. {{ range .Import }}
  16. import "{{.}}"
  17. {{ end }}
  18. import "io"
  19. import "os"
  20. import "fmt"
  21. {{$prefix := .Prefix }}
  22. {{- $Parser := ( printf "%s%s" $prefix "Parser") -}}
  23. {{- $ParserError := ( printf "%s%s" $prefix "ParserError") -}}
  24. {{- $Lexer := ( printf "%s%s" $prefix "Lexer") -}}
  25. {{- $TokenKind := ( printf "%s%s" $prefix "TokenKind") -}}
  26. {{- $Position := ( printf "%s%s" $prefix "Position") -}}
  27. {{- $Token := ( printf "%s%s" $prefix "Token") -}}
  28. {{- $Value := ( printf "%s%s" $prefix "Value") -}}
  29. // {{$Value}} is the lexical value of a lexer token.
  30. {{if .ValueType }}
  31. type {{$Value}} = {{.ValueType}}
  32. {{ else }}
  33. // This is based on strings as a default.
  34. type {{$Value}} = string
  35. {{ end }}
  36. {{if (.LexerType) eq "scanner.Scanner"}}
  37. // {{$Position}} is a position within a source file. Since the lexer is based on
  38. // text/scanner, we use that package's Position.
  39. type {{$Position}} = scanner.Position
  40. {{else}}
  41. // {{$Position}} is a position within a source file.
  42. type {{$Position}} struct {
  43. Filename string // filename, if any
  44. Offset int // byte offset, starting at 0
  45. Line int // line number, starting at 1
  46. Column int // column number, starting at 1 (character count per line)
  47. }
  48. {{end}}
  49. // {{$TokenKind}} is the kind or type of a token.
  50. // This has rune as the underlying type so one-character tokens can be easily
  51. // supported. EOF will be 65535 (I.e, -1 cast to rune). Non-character token
  52. // kinds will start from 65533 down (i.e -3, -4, -5, etc).
  53. type {{$TokenKind}} rune
  54. // No{{$TokenKind}} means "no token kind" i.e. no token.
  55. const No{{$TokenKind}} {{$TokenKind}} = {{$TokenKind}}(0)
  56. // {{$TokenKind}}EOF means the end of the input.
  57. const {{$TokenKind}}EOF {{$TokenKind}} = {{$TokenKind}}(-1)
  58. // {{$TokenKind}}Error means a parsing or lexing error was encountered.
  59. const {{$TokenKind}}Error {{$TokenKind}} = {{$TokenKind}}(-2)
  60. // Convert token kind to a string representation
  61. func (tk {{$TokenKind}}) String() string {
  62. {{if (.LexerType) eq "scanner.Scanner"}}
  63. return scanner.TokenString(rune(tk))
  64. {{else}}
  65. switch (tk) {
  66. case No{{$TokenKind}}: return "NoToken"
  67. case {{$TokenKind}}EOF: return "EOF"
  68. {{ range .Grammar.Rules -}}
  69. {{- $ruleName := .Name -}}
  70. {{- if .IsTerminal -}}
  71. {{- $TokenKindName := ( printf "%s%s" $TokenKind $ruleName) -}}
  72. case {{$TokenKindName}}: return "{{$TokenKindName}}"
  73. {{end}}
  74. {{end}}
  75. default:
  76. return fmt.Printf("TokenKind(%d)", int(tk))
  77. }
  78. {{end}}
  79. }
  80. // {{$Token}} is the result of a single lexical analysis step by the lexer.
  81. type {{$Token}} struct {
  82. {{$Position}} // Position in the source where the token was found.
  83. {{$TokenKind}} // Type of the token
  84. {{$Value}} // Value of the token
  85. }
  86. // Make{{$Token}} makes a token with the given position, type and value.
  87. func Make{{$Token}}(pos {{$Position}}, typ {{$TokenKind}}, val {{$Value}}) {{$Token}} {
  88. return {{$Token}}{ pos, typ, val}
  89. }
  90. // {{$Lexer}} performs the lexical analysis of the input.
  91. type {{$Lexer}} struct {
  92. // Embed {{.LexerType}}
  93. {{.LexerType}}
  94. Filename string
  95. }
  96. {{if (.LexerType) eq "scanner.Scanner"}}
  97. // New{{$Lexer}}FromReader creates a new lexer for the given parser and input.
  98. func New{{$Lexer}}FromReader(parser *{{$Parser}}, reader io.Reader, filename string) *{{$Lexer}} {
  99. lexer := &{{$Lexer}}{}
  100. lexer.Filename = filename
  101. lexer.Scanner.Init(reader)
  102. lexer.Scanner.Mode = scanner.GoTokens
  103. lexer.Scanner.Error = func (s *scanner.Scanner, msg string) {
  104. parser.Panicf("%s: scanner error: %s, %s", s.Position, s.TokenText(), msg)
  105. }
  106. // XXX: needs to be generated from the identifier rule in the syntax!
  107. lexer.Scanner.IsIdentRune = func(ch rune, i int) bool {
  108. if i == 0 {
  109. return unicode.IsLetter(ch)
  110. }
  111. return unicode.IsLetter(ch) ||
  112. unicode.IsNumber(ch) ||
  113. ch == '_' ||
  114. ch == '-'
  115. }
  116. return lexer
  117. }
  118. func (lex *{{$Lexer}}) Lex() {{$Token}} {
  119. scanned := lex.Scanner.Scan()
  120. pos := lex.Scanner.Position
  121. pos.Filename = lex.Filename
  122. value := lex.Scanner.TokenText()
  123. // Get rid of the quotes
  124. if scanned == scanner.Char ||
  125. scanned == scanner.String ||
  126. scanned == scanner.RawString {
  127. value = value[1:len(value) - 1]
  128. }
  129. token := {{$Token}} {
  130. {{$TokenKind}}: {{$TokenKind}}(scanned),
  131. {{$Value}}: value,
  132. {{$Position}}: pos,
  133. }
  134. return token
  135. }
  136. {{else}}
  137. // Please provide the following functions:
  138. //
  139. // * You own lexer creation function with the following signature:
  140. // New{{$Lexer}}FromReader(parser *{{$Parser}}, reader io.Reader, filename string) *{{$Lexer}}
  141. //
  142. // * Your own lexing function with the type
  143. // func (lex *{{$Lexer}}) Lex() {{$Token}}
  144. {{end}}
  145. // {{$Parser}} parses the input and returns a parse tree,
  146. // based on the rules in {{.InName}}
  147. type {{$Parser}} struct {
  148. reader io.Reader
  149. lexer *{{$Lexer}}
  150. current {{$Token}}
  151. Errors []{{$ParserError}}
  152. Filename string
  153. Debug io.Writer
  154. }
  155. func New{{$Parser}}FromReader(reader io.Reader, filename string, debug bool) *{{$Parser}} {
  156. parser := &{{$Parser}}{}
  157. parser.lexer = New{{$Lexer}}FromReader(parser, reader, filename)
  158. parser.Filename = filename
  159. parser.current.{{$TokenKind}} = No{{$TokenKind}}
  160. parser.Debug = nil
  161. if debug {
  162. parser.Debug = os.Stderr
  163. }
  164. return parser
  165. }
  166. // Advances the parser. Returns the current token /after/ advancing.
  167. func (p *{{$Parser}}) Advance() {{$Token}} {
  168. token := p.lexer.Lex()
  169. p.Debugf("Lexed token: %v", token)
  170. p.current = token
  171. return token
  172. }
  173. // {{$ParserError}} is an error encountered during parsing or lexing.
  174. // The parser may panic with this type on errors that would prevent the parser
  175. // from making progress.
  176. type {{$ParserError}} struct {
  177. *{{$Parser}} // Parser that had the error.
  178. *{{$Token}} // Token at which the error was found
  179. Chain error // underlying error
  180. }
  181. func (pe {{$ParserError}}) Error() string {
  182. // XXX will need to be improved
  183. return pe.Chain.Error()
  184. }
  185. func (parser *{{$Parser}}) Errorf(message string, args ...interface{}) {{$ParserError}} {
  186. err := fmt.Errorf(message, args...)
  187. pe := {{$ParserError}} {
  188. {{$Parser}}: parser,
  189. {{$Token}}: &parser.current,
  190. Chain: err,
  191. }
  192. parser.Errors = append(parser.Errors, pe)
  193. return pe
  194. }
  195. func (parser *{{$Parser}}) Panicf(message string, args ...interface{}) {
  196. pe := parser.Errorf(message, args...)
  197. panic(pe)
  198. }
  199. func (p *{{$Parser}}) Debugf(message string, args ...interface{}) {
  200. if p.Debug != nil {
  201. fmt.Fprintf(p.Debug, message, args)
  202. }
  203. }
  204. /* Looks at the current token and advances the lexer if the token is of any of
  205. the token kinds given in kinds. In this case it will return the accepted
  206. token and advance the parser. Otherwise, it will call parser.Panicf.*/
  207. func (parser *{{$Parser}}) Require(kinds ...{{$TokenKind}}) {{$Token}} {
  208. parser.Debugf("Require: %v\n", kinds)
  209. if parser.current.{{$TokenKind}} == {{$TokenKind}}(0) {
  210. parser.Advance()
  211. }
  212. expected := ""
  213. sep := ""
  214. for _, kind := range kinds {
  215. if kind == parser.current.{{$TokenKind}} {
  216. accepted := parser.current
  217. parser.Advance()
  218. return accepted
  219. }
  220. expected = fmt.Sprintf("%s%s%s", expected, sep, kind.String())
  221. }
  222. parser.Panicf("error: expected one of the following: %s", expected)
  223. return {{$Token}}{}
  224. }
  225. func (parser {{$Parser}}) NextIs(kinds ...{{$TokenKind}}) bool {
  226. parser.Debugf("NextIs: %v\n", kinds)
  227. if (parser.current.{{$TokenKind}} == 0) {
  228. parser.Advance()
  229. }
  230. for _, kind := range kinds {
  231. if kind == parser.current.{{$TokenKind}} {
  232. return true
  233. }
  234. }
  235. return false
  236. }
  237. {{ $tokenKindValue := 2 }}
  238. {{ range .Grammar.Rules -}}
  239. {{- $ruleName := .Name -}}
  240. {{ if .Template }}
  241. // Expanded from template of rule {{$ruleName}}
  242. {{ .Template }}
  243. {{ end }}
  244. {{- $terminal := .IsTerminal -}}
  245. {{- if $ -}}
  246. {{- $TokenKindName := ( printf "%s%s" $TokenKind $ruleName) -}}
  247. const {{$TokenKindName}} {{$TokenKind}} = {{$TokenKind}}(-{{$tokenKindValue}})
  248. {{ $tokenKindValue = (iadd $tokenKindValue 1) }}
  249. func ( *{{$Lexer}}) Lex{{$TokenKindName}}() ({{$TokenKind}}, error) {
  250. result := {{$TokenKindName}}
  251. return result, nil
  252. }
  253. {{ else }}
  254. {{ $RuleType := ( printf "%s%s" $prefix $ruleName) }}
  255. type {{$RuleType}} struct {
  256. {{$Token}}
  257. {{ range .Definition.Sequences -}}
  258. {{- range .Elements -}}
  259. {{- if (IsNonterminal .) -}}
  260. *{{$prefix}}{{ . }}
  261. {{ end -}}
  262. {{- end}}
  263. {{ end -}}
  264. }
  265. func (p *{{$Parser}}) Is{{$RuleType}}() (bool) {
  266. return false
  267. // return p.NextIs()
  268. }
  269. func (p *{{$Parser}}) Parse{{$RuleType}}() (*{{$RuleType}}, error) {
  270. var err error
  271. result := &{{$RuleType}} { {{$Token}}: p.current }
  272. {{ range .Definition.Sequences -}}
  273. {{- range .Elements -}}
  274. {{- if (IsNonterminal .) -}}
  275. {{- $fn := (printf "%s%s" $prefix .)}}
  276. if p.Is{{$fn}}() {
  277. result.{{$fn}}, err = p.Parse{{$fn}}()
  278. if err != nil {
  279. return result, err
  280. }
  281. }
  282. {{ end -}}
  283. {{- end}}
  284. {{ end -}}
  285. return result, err
  286. }
  287. {{end}}
  288. {{ end }}