flexer_lexer.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package flexgen
  2. import "src.eruta.nl/beoran/ll1/common"
  3. import "src.eruta.nl/beoran/ll1/flexer"
  4. const (
  5. FlexgenKindDot = common.Kind(-1 - iota)
  6. FlexgenKindLiteralString
  7. FlexgenKindLiteralRaw
  8. FlexgenKindLiteralChar
  9. FlexgenKindTerminal
  10. FlexgenKindArrow
  11. FlexgenKindKeyword
  12. FlexgenKindWhitespace
  13. FlexgenKindLineComment
  14. FlexgenKindBlockComment
  15. FlexgenKindAction
  16. )
  17. /*
  18. // common generator's input own lexer specification
  19. dot -> '\.' .
  20. arrow -> "(?:->|→)" .
  21. terminal -> "[[:isLower:]][[:isAlNum]_-]+" .
  22. literal-string -> `"` flex-esc-string .
  23. literal-raw -> "`" flex-string .
  24. literal-char -> "'" flex-string .
  25. common-keyword -> "flex-skip|flex-string|flex-esc-string" .
  26. whitespace -> "[ \t\n\r]+" flex-skip .
  27. line-comment -> "//[^\n\r]+" flex-skip .
  28. block-comment -> "/\*(?ms:.)*?\* /" .
  29. common-action -> `%{(?ms:.)*?}%` .
  30. */
  31. type CheckedError struct {
  32. Error error
  33. }
  34. func Check(err error) {
  35. if err != nil {
  36. panic(CheckedError{err})
  37. }
  38. }
  39. func LexcommonInputString(name, input string) []common.Token {
  40. f := flexer.NewFlexer(name, input)
  41. Check(f.Lexeme(FlexgenKindWhitespace, `[ \t\n\r]+`, "", nil))
  42. Check(f.Lexeme(FlexgenKindLineComment, `//[^\n\r]+[\n\r]+`, "", nil))
  43. Check(f.Lexeme(FlexgenKindBlockComment, `/\*(?ms:.)*?\*/`, "", nil))
  44. Check(f.Lexeme(FlexgenKindDot, `\.`, "", nil))
  45. Check(f.Lexeme(FlexgenKindArrow, `(?:->|→)`, "", nil))
  46. Check(f.Lexeme(FlexgenKindTerminal, `[[:lower:]][[:alnum:]_-]+`, "", nil))
  47. f.EscapedStringLexeme(FlexgenKindLiteralString, `"`, `"`, "literal-string")
  48. f.RawStringLexeme(FlexgenKindLiteralRaw, "`", "`", "literal-raw")
  49. f.RawStringLexeme(FlexgenKindLiteralChar, `''`, `''`, "literal-char")
  50. Check(f.Lexeme(FlexgenKindKeyword, `flex-skip|flex-string|flex-esc-string`, "", nil))
  51. Check(f.Lexeme(FlexgenKindAction, `@{(?ms:.)*?}@`, "", nil))
  52. skipKinds := []common.Kind{common.SkipKind, FlexgenKindWhitespace, FlexgenKindBlockComment, FlexgenKindLineComment}
  53. return flexer.LexAll(f, skipKinds...)
  54. }