common.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Common interfaces and shared functions for the ll1 module.
  2. package common
  3. import "regexp"
  4. import "strings"
  5. import "fmt"
  6. type Location struct {
  7. Name *string
  8. Line int
  9. Col int
  10. }
  11. func (p Location) String() string {
  12. name := "<input>"
  13. if p.Name != nil {
  14. name = *p.Name
  15. }
  16. return fmt.Sprintf("%s:%d:%d:", name, p.Line, p.Col)
  17. }
  18. // Kind is the kind of token.
  19. type Kind int
  20. const (
  21. EndKind Kind = -29000
  22. SkipKind Kind = -30000
  23. ErrorKind Kind = -31000
  24. )
  25. type Action func(f Lexer, k Kind, matches ...string) []Token
  26. // Value is the value of a token, can be string value, integer
  27. // or some other custom value
  28. type Value interface {
  29. Value() Value
  30. String() string
  31. }
  32. type StringValue string
  33. func (sv StringValue) Value() Value {
  34. return sv
  35. }
  36. func (sv StringValue) String() string {
  37. return string(sv)
  38. }
  39. type ErrorValue struct {
  40. Err error
  41. }
  42. func (ev ErrorValue) Value() Value {
  43. return ev
  44. }
  45. func (ev ErrorValue) String() string {
  46. return string(ev.Err.Error())
  47. }
  48. type IntValue int64
  49. func (iv IntValue) Value() Value {
  50. return iv
  51. }
  52. func (iv IntValue) String() string {
  53. return fmt.Sprintf("%d", iv)
  54. }
  55. type FloatValue int64
  56. func (fv FloatValue) Value() Value {
  57. return fv
  58. }
  59. func (fv FloatValue) String() string {
  60. return fmt.Sprintf("%d", fv)
  61. }
  62. type Token interface {
  63. Location() Location
  64. Kind() Kind
  65. Text() string
  66. Value() Value
  67. }
  68. type Lexer interface {
  69. // Accept will accept a regexp and advance, returning the matches.
  70. // Returns nil if no matches were found.
  71. Accept(re *regexp.Regexp) []string
  72. // Returns the current lexer Location.
  73. Location() Location
  74. // Returns if the lexer is at the end or not.
  75. EOF() bool
  76. // The lexer creates a token with the current lexer Location and
  77. // the given kind and text.
  78. MakeToken(kind Kind, form string, args ...interface{}) Token
  79. // The lexer creates a token with the current lexer Location and
  80. // the given kind. The text is taken from the lexer string builder and
  81. // that builser is reset.
  82. MakeBuilderToken(kind Kind) Token
  83. // The lexer has a string builder, which can be used to append
  84. // strings or runes to and which can be returned and cleared when the
  85. // token is complete.
  86. Builder() *strings.Builder
  87. // Lexeme adds a lexeme to the lexer.
  88. Lexeme(kind Kind, re, context string, act Action) error
  89. // Calls the lexer once.
  90. LexOnce() []Token
  91. // Returns the current lexer context
  92. Context() string
  93. // Pushes the named context on the lexer context stack
  94. PushContext(name string)
  95. // Pops the current context from the lexer context stack.
  96. PopContext()
  97. }