common.go 2.5 KB

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