parser.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Uses an ll1 grammar to parse an input from an ll1 flexer to an Ast.
  2. package parser
  3. // import "fmt"
  4. import . "src.eruta.nl/beoran/ll1/common"
  5. import . "src.eruta.nl/beoran/ll1/ast"
  6. import . "src.eruta.nl/beoran/ll1/grammar"
  7. import . "src.eruta.nl/beoran/ll1/flexer"
  8. // A TokenMapper maps a token to a Species.
  9. type TokenMapper interface {
  10. // Returns the species to use for the grammar rule. If nil, the token
  11. // may be skipped.
  12. Map(t Rule) Species
  13. }
  14. type DefaultMapper struct{}
  15. func (DefaultMapper) Map(r Rule) Species {
  16. return MakeSpecies(r.Name())
  17. }
  18. type Parser struct {
  19. Grammar
  20. TokenMapper
  21. Tokens []Token
  22. Index int
  23. Errors []error
  24. Result Ast
  25. // current location in the ast being built.
  26. Now Ast
  27. Recursion int
  28. }
  29. func (p *Parser) MakeError(msg string, args ...interface{}) error {
  30. return MakeErrorToken(p.Token().Location(), msg, args...)
  31. }
  32. func (p *Parser) AddError(msg string, args ...interface{}) error {
  33. err := MakeErrorToken(p.Token().Location(), msg, args...)
  34. p.Errors = append(p.Errors, err)
  35. return err
  36. }
  37. func (g Parser) Token() Token {
  38. if g.Index < len(g.Tokens) {
  39. return g.Tokens[g.Index]
  40. }
  41. last := g.Tokens[len(g.Tokens)-1]
  42. return MakeErrorToken(last.Location(), "Unexpected end of input.")
  43. }
  44. func (g *Parser) Expect(kind Kind) Token {
  45. tok := g.Token()
  46. if tok.Kind() == kind {
  47. g.Index++
  48. return tok
  49. }
  50. return nil
  51. }
  52. func (g *Parser) Accept(kinds ...Kind) Token {
  53. tok := g.Token()
  54. for _, kind := range kinds {
  55. if tok.Kind() == kind {
  56. g.Index++
  57. return tok
  58. }
  59. }
  60. return nil
  61. }
  62. func (g *Parser) Require(kinds ...Kind) Token {
  63. tok := g.Accept(kinds...)
  64. if tok != nil {
  65. return tok
  66. }
  67. g.AddError("Expected: %v", kinds)
  68. return nil
  69. }
  70. func (p *Parser) ParseTerminal(term Terminal) error {
  71. tok := p.Expect(term.Kind)
  72. if tok == nil {
  73. return p.MakeError("Expected token kind: %d", term.Kind)
  74. }
  75. spec := p.Map(term)
  76. NewChild(p.Now, spec, tok)
  77. // now should alreqdy be of suitable kind to accept a terminal token child.
  78. return nil
  79. }
  80. func (g *Parser) ParseEpsilon(term Epsilon) error {
  81. return nil
  82. }
  83. func (p *Parser) ParseEnd(term EndOfInput) error {
  84. if p.Index >= (len(p.Tokens) - 1) {
  85. p.Index = len(p.Tokens) // reach the end.
  86. return nil
  87. } else {
  88. return p.MakeError("Expected end of input.")
  89. }
  90. }
  91. func (p *Parser) PopUp() {
  92. if p.Now != nil && p.Now.Parent() != nil {
  93. p.Now = p.Now.Parent()
  94. }
  95. }
  96. func (p *Parser) ParseReference(ref Reference) error {
  97. rule, err := ref.Resolve()
  98. if err != nil {
  99. return err
  100. }
  101. return p.ParseRule(rule)
  102. }
  103. func (p *Parser) ParseSequence(seq Sequence) error {
  104. spec := p.Map(seq)
  105. p.Now = NewChild(p.Now, spec, p.Token())
  106. defer p.PopUp()
  107. for _, rule := range seq.Rules {
  108. err := p.ParseRule(rule)
  109. if err != nil {
  110. p.Errors = append(p.Errors, err)
  111. return err
  112. }
  113. }
  114. return nil
  115. }
  116. /*
  117. func (p *Parser) ParseOptinalAlternates(alt Alternates) error {
  118. for _, rule := range alt.Rules {
  119. if IsReference(rule) {
  120. }
  121. err := p.ParseRule(rule)
  122. if err != nil {
  123. errors = append(errors, err)
  124. } else { // one alternate was OK here.
  125. return nil
  126. }
  127. }
  128. // If we get here no alternate was ok.
  129. p.Errors = append(p.Errors, errors...)
  130. return p.MakeError("Could not parse alternate.")
  131. }
  132. */
  133. func (p *Parser) ParseAlternates(alt Alternates) error {
  134. spec := p.Map(alt)
  135. p.Now = NewChild(p.Now, spec, p.Token())
  136. defer p.PopUp()
  137. errors := []error{}
  138. hasEpsilon := false
  139. for _, rule := range alt.Rules {
  140. if IsEpsilon(rule) {
  141. hasEpsilon = true
  142. } else {
  143. first, _ := rule.FirstSet()
  144. if first.ContainsKind(p.Token().Kind()) {
  145. err := p.ParseRule(rule)
  146. if err != nil {
  147. errors = append(errors, err)
  148. } else { // this alternate was OK here.
  149. return nil
  150. }
  151. }
  152. }
  153. }
  154. if hasEpsilon { // No match is ok.
  155. return nil
  156. }
  157. // If we get here no alternate was ok.
  158. p.Errors = append(p.Errors, errors...)
  159. return p.MakeError("Could not parse alternate.")
  160. }
  161. func (p *Parser) ParseRule(r Rule) error {
  162. p.Recursion++
  163. if p.Recursion > 800 {
  164. panic("Too much recursion in grammar.")
  165. }
  166. switch rv := r.(type) {
  167. case Epsilon:
  168. p.ParseEpsilon(rv)
  169. case EndOfInput:
  170. p.ParseEnd(rv)
  171. case Terminal:
  172. p.ParseTerminal(rv)
  173. case Alternates:
  174. p.ParseAlternates(rv)
  175. case Sequence:
  176. p.ParseSequence(rv)
  177. case Reference:
  178. p.ParseReference(rv)
  179. default:
  180. panic("Unknown rule type in ParseRule.")
  181. }
  182. // p.Now.AddChild()
  183. return nil
  184. }
  185. func (p *Parser) Parse(input []Token) error {
  186. p.Tokens = input
  187. p.Recursion = 0
  188. p.Errors = []error{}
  189. p.Result = NewAstNone()
  190. p.Now = p.Result
  191. return p.ParseRule(p.Grammar.Top)
  192. return nil
  193. }