lexer.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. package muesli
  2. import (
  3. "bufio"
  4. _ "bytes"
  5. _ "errors"
  6. "fmt"
  7. "io"
  8. _ "io"
  9. "os"
  10. _ "reflect"
  11. _ "runtime"
  12. "strconv"
  13. "strings"
  14. "unicode"
  15. _ "unicode"
  16. // "gitlab.com/beoran/woe/graphviz"
  17. // _ "gitlab.com/beoran/woe/monolog"
  18. )
  19. /* A Lexer splits scanned input into tokens.
  20. */
  21. type Lexer struct {
  22. Position
  23. Index int
  24. Start int
  25. io.RuneScanner
  26. buffer []rune
  27. Current rune
  28. Keywords map[string]*Keyword
  29. LoggerWrapper
  30. }
  31. func (lexer *Lexer) SetLogger(logger Logger) {
  32. lexer.LoggerWrapper = LoggerWrapper{logger}
  33. }
  34. func (lexer *Lexer) ClearBuffer() {
  35. lexer.buffer = make([]rune, 0)
  36. }
  37. func (lexer *Lexer) MakeIntegerToken() Token {
  38. var sbuffer = string(lexer.buffer)
  39. i, err := strconv.ParseInt(sbuffer, 0, 64)
  40. if err == nil {
  41. lexer.ClearBuffer()
  42. return NewToken(TokenKindInteger, IntValue(i), lexer.Position)
  43. } else {
  44. lexer.ClearBuffer()
  45. return lexer.MakeErrorToken(err)
  46. }
  47. }
  48. func (lexer *Lexer) MakeTokenFromKeyword(kw * Keyword) Token {
  49. lexer.ClearBuffer()
  50. return NewToken(kw.TokenKind, kw.Value, lexer.Position)
  51. }
  52. func (lexer *Lexer) MakeFloatToken() Token {
  53. var sbuffer = string(lexer.buffer)
  54. f, err := strconv.ParseFloat(sbuffer, 64)
  55. if err == nil {
  56. lexer.ClearBuffer()
  57. return NewToken(TokenKindFloat, FloatValue(f), lexer.Position)
  58. } else {
  59. lexer.ClearBuffer()
  60. return lexer.MakeErrorToken(err)
  61. }
  62. }
  63. func (lexer *Lexer) MakeBooleanToken(b bool) Token {
  64. lexer.ClearBuffer()
  65. if b {
  66. return NewToken(TokenKindBoolean, TrueValue, lexer.Position)
  67. } else {
  68. return NewToken(TokenKindBoolean, FalseValue, lexer.Position)
  69. }
  70. }
  71. func (lexer *Lexer) MakeNilToken() Token {
  72. lexer.ClearBuffer()
  73. return NewToken(TokenKindNil, NilValue, lexer.Position)
  74. }
  75. func (lexer *Lexer) MakeBuiltinToken() Token {
  76. var sbuffer = string(lexer.buffer)
  77. lexer.ClearBuffer()
  78. if sbuffer == "true" {
  79. lexer.ClearBuffer()
  80. return NewToken(TokenKindBoolean, TrueValue, lexer.Position)
  81. } else if sbuffer == "false" {
  82. return NewToken(TokenKindBoolean, FalseValue, lexer.Position)
  83. } else if sbuffer == "nil" {
  84. return NewToken(TokenKindNil, NilValue, lexer.Position)
  85. } else {
  86. return lexer.MakeErrorfToken("Not a builtin: %s", sbuffer)
  87. }
  88. }
  89. func (lexer *Lexer) MakeStringValueToken(kind TokenKind) Token {
  90. var sbuffer = string(lexer.buffer)
  91. return NewToken(kind, StringValue(sbuffer), lexer.Position)
  92. }
  93. func (lexer *Lexer) MakeTypeValueToken(kind TokenKind) Token {
  94. var sbuffer = string(lexer.buffer)
  95. return NewToken(kind, TypeValue(sbuffer), lexer.Position)
  96. }
  97. func (lexer *Lexer) MakeErrorValueToken(kind TokenKind) Token {
  98. var sbuffer = string(lexer.buffer)
  99. return NewToken(kind, NewErrorValuef("%s", sbuffer), lexer.Position)
  100. }
  101. func (lexer *Lexer) MakeWordValueToken(kind TokenKind) Token {
  102. var sbuffer = string(lexer.buffer)
  103. return NewToken(kind, WordValue(sbuffer), lexer.Position)
  104. }
  105. func (lexer *Lexer) MakeToken(kind TokenKind) Token {
  106. switch kind {
  107. case TokenKindInteger:
  108. return lexer.MakeIntegerToken()
  109. case TokenKindFloat:
  110. return lexer.MakeFloatToken()
  111. case TokenKindString:
  112. return lexer.MakeStringValueToken(kind)
  113. case TokenKindSymbol:
  114. return lexer.MakeWordValueToken(kind)
  115. case TokenKindType:
  116. return lexer.MakeTypeValueToken(kind)
  117. case TokenKindError:
  118. return lexer.MakeErrorValueToken(kind)
  119. case TokenKindWord:
  120. return lexer.MakeWordValueToken(kind)
  121. case TokenKindOperator:
  122. fallthrough
  123. case TokenKindRedirect:
  124. fallthrough
  125. case TokenKindMethod:
  126. return lexer.MakeWordValueToken(kind)
  127. case TokenKindNil:
  128. fallthrough
  129. case TokenKindBoolean:
  130. return lexer.MakeBuiltinToken()
  131. case TokenKindGet:
  132. fallthrough
  133. case TokenKindSet:
  134. fallthrough
  135. case TokenKindOpenBlock:
  136. fallthrough
  137. case TokenKindCloseBlock:
  138. fallthrough
  139. case TokenKindOpenList:
  140. fallthrough
  141. case TokenKindCloseList:
  142. fallthrough
  143. case TokenKindOpenParen:
  144. fallthrough
  145. case TokenKindCloseParen:
  146. fallthrough
  147. case TokenKindEOX:
  148. fallthrough
  149. case TokenKindEOF:
  150. val := StringValue(string(lexer.buffer))
  151. lexer.ClearBuffer()
  152. return NewToken(kind, val, lexer.Position)
  153. default:
  154. return lexer.MakeErrorfToken("Internal error on token type %s", kind)
  155. }
  156. }
  157. func (lexer Lexer) MakeErrorToken(err error) Token {
  158. return NewToken(TokenKindError, ErrorValue{err}, lexer.Position)
  159. }
  160. func (lexer Lexer) MakeErrorfToken(format string, va ...interface{}) Token {
  161. err := fmt.Errorf(format, va...)
  162. return lexer.MakeErrorToken(err)
  163. }
  164. func (lexer Lexer) MakeEOFToken() Token {
  165. return NewToken(TokenKindEOF, &EmptyValue{}, lexer.Position)
  166. }
  167. func (lexer *Lexer) Peek() (rune, error) {
  168. r, _, err := lexer.RuneScanner.ReadRune()
  169. err2 := lexer.RuneScanner.UnreadRune()
  170. if err == nil {
  171. err = err2
  172. }
  173. return r, err
  174. }
  175. /* Advances the lexer's position based on the rune r read. */
  176. func (lexer *Lexer) advance(r rune) {
  177. lexer.Current = r
  178. lexer.Index++
  179. lexer.Position.Column++
  180. if r == '\n' {
  181. lexer.Position.Column = 1
  182. lexer.Position.Line++
  183. }
  184. }
  185. /* Append a rune to the lexer's buffer. */
  186. func (lexer *Lexer) appendRune(r rune) {
  187. lexer.buffer = append(lexer.buffer, r)
  188. }
  189. /* Advances the lexer's input buffer but does not store the rune read,
  190. * but just returns it. */
  191. func (lexer *Lexer) Skip() (rune, error) {
  192. r, _, err := lexer.RuneScanner.ReadRune()
  193. if err != nil {
  194. return 0, err
  195. }
  196. lexer.advance(r)
  197. return r, nil
  198. }
  199. /* Actually reads the next rune from the lexer's input source and stores
  200. * them in the lexer's token buffer.
  201. * Shorthand for r, err := lexer.Skip() ; lexer.appendRune(r) */
  202. func (lexer *Lexer) Next() (rune, error) {
  203. r, err := lexer.Skip()
  204. if err == nil {
  205. lexer.appendRune(r)
  206. }
  207. return r, nil
  208. }
  209. func (lexer *Lexer) DoIf(predicate func(rune) bool,
  210. todo func(*Lexer) (rune, error)) (bool, error) {
  211. r, err := lexer.Peek()
  212. if err != nil {
  213. return false, err
  214. }
  215. if predicate(r) {
  216. r, err = todo(lexer)
  217. if err != nil {
  218. return true, err
  219. }
  220. return true, nil
  221. }
  222. return false, nil
  223. }
  224. func (lexer *Lexer) NextIf(predicate func(rune) bool) (bool, error) {
  225. return lexer.DoIf(predicate, (*Lexer).Next)
  226. }
  227. func (lexer *Lexer) SkipIf(predicate func(rune) bool) (bool, error) {
  228. return lexer.DoIf(predicate, (*Lexer).Skip)
  229. }
  230. func (lexer *Lexer) NextWhile(predicate func(rune) bool) (bool, error) {
  231. result := true
  232. ok, err := lexer.NextIf(predicate)
  233. result = result || ok
  234. for ; ok && (err == nil); ok, err = lexer.NextIf(predicate) {
  235. result = result || ok
  236. }
  237. return result, err
  238. }
  239. func (lexer *Lexer) SkipWhile(predicate func(rune) bool) (bool, error) {
  240. result := true
  241. ok, err := lexer.SkipIf(predicate)
  242. result = result || ok
  243. for ; ok && (err == nil); ok, err = lexer.SkipIf(predicate) {
  244. result = result || ok
  245. }
  246. return result, err
  247. }
  248. func isEOX(r rune) bool {
  249. return r == '\n' || r == '.'
  250. }
  251. func isSpace(r rune) bool {
  252. return r == ' ' || r == '\t' || r == '\v' || r == '\r'
  253. }
  254. func isSpaceOrEOX(r rune) bool {
  255. return r == ' ' || r == '\t' || r == '\v' || r == '\r' || r == '\n' || r == '.'
  256. }
  257. func isComment(r rune) bool {
  258. return r == '#'
  259. }
  260. func isOperator(r rune) bool {
  261. return isPureOperator(r) || isRedirect(r) || isMethod(r)
  262. }
  263. func isPureOperator(r rune) bool {
  264. return r == '+' || r == '-' || r == '*' || r == '/' || r == '^' ||
  265. r == '%' || r == '~'
  266. }
  267. func isRedirect(r rune) bool {
  268. return r == '|' || r == '&' || r == '>' || r == '<' || r == '@'
  269. }
  270. func isMethod(r rune) bool {
  271. return r == ',' || r == ';'
  272. }
  273. func (lexer *Lexer) SkipSpace() error {
  274. _, err := lexer.SkipWhile(isSpace)
  275. return err
  276. }
  277. func (lexer *Lexer) SkipBlockComment() error {
  278. var err error
  279. var r rune
  280. lexer.LogDebug("Skipping block comment.")
  281. for block := 1; block > 0 && err == nil; {
  282. _, err = lexer.Skip()
  283. if err != nil {
  284. return err
  285. }
  286. r, err = lexer.Peek()
  287. if r == '{' {
  288. block++
  289. } else if r == '}' {
  290. block--
  291. }
  292. lexer.LogDebug("Skipping block comment: %d", block)
  293. }
  294. _, err = lexer.Skip()
  295. return err
  296. }
  297. func (lexer *Lexer) SkipComment() error {
  298. r, err := lexer.Skip()
  299. lexer.LogDebug("Skipping %c.", r)
  300. if err != nil {
  301. return err
  302. }
  303. r, err = lexer.Peek()
  304. if r == '{' {
  305. return lexer.SkipBlockComment()
  306. }
  307. for r != '\n' && err == nil {
  308. lexer.LogDebug("Skipping comment %c.", r)
  309. _, err = lexer.Skip()
  310. if err != nil {
  311. return err
  312. }
  313. r, err = lexer.Peek()
  314. }
  315. if err != nil {
  316. return err
  317. }
  318. _, err = lexer.Skip()
  319. return err
  320. }
  321. /* Handles errors including EOF by either returning an error token or an
  322. * EOF token.
  323. */
  324. func (lexer *Lexer) handleError(err error) Token {
  325. if err == io.EOF {
  326. return lexer.MakeEOFToken()
  327. } else {
  328. return lexer.MakeErrorToken(err)
  329. }
  330. }
  331. func (lexer *Lexer) LexOperator() Token {
  332. _, err := lexer.NextWhile(isOperator)
  333. if err != nil {
  334. return lexer.MakeErrorfToken("when parsing operator: %s", err)
  335. }
  336. oper := lexer.buffer[0]
  337. switch {
  338. case isPureOperator(oper): return lexer.MakeToken(TokenKindOperator)
  339. case isRedirect(oper): return lexer.MakeToken(TokenKindRedirect)
  340. case isMethod(oper): return lexer.MakeToken(TokenKindMethod)
  341. }
  342. return lexer.MakeToken(TokenKindOperator)
  343. }
  344. func (lexer *Lexer) LexNumber() Token {
  345. isFloat := false
  346. maybeOperator := false
  347. // skip any first - or +
  348. _, err := lexer.NextIf(func(r rune) bool {
  349. maybeOperator = (r == '-' || r == '+') // it might also be an operator in stead.
  350. return r == '-' || r == '+'
  351. })
  352. if err != nil {
  353. return lexer.MakeErrorfToken("Error during parsing of number prefix: %s", err)
  354. }
  355. for {
  356. r, err := lexer.Peek()
  357. if err != nil {
  358. return lexer.MakeErrorfToken("Error during parsing of number: %s", err)
  359. }
  360. if unicode.IsDigit(r) {
  361. maybeOperator = false
  362. lexer.Next()
  363. } else if r == '.' {
  364. if isFloat {
  365. lexer.Next()
  366. return lexer.MakeErrorfToken("two points in floating point number")
  367. } else {
  368. isFloat = true
  369. lexer.Next()
  370. }
  371. } else if maybeOperator {
  372. return lexer.LexOperator()
  373. } else {
  374. break
  375. }
  376. }
  377. if isFloat {
  378. return lexer.MakeToken(TokenKindFloat)
  379. } else {
  380. return lexer.MakeToken(TokenKindInteger)
  381. }
  382. }
  383. func isDoubleQuote(r rune) bool {
  384. return r == '"'
  385. }
  386. func (lexer *Lexer) handleEscapeHexChars(amount int) error {
  387. buffer := make([]byte, 0)
  388. r, err := lexer.Skip()
  389. for index := 0; err == nil && index < amount; {
  390. if unicode.Is(unicode.ASCII_Hex_Digit, r) {
  391. buffer = append(buffer, byte(r))
  392. } else {
  393. return fmt.Errorf("Not a hexadecimal digit: %c", r)
  394. }
  395. index++
  396. if index < amount {
  397. r, err = lexer.Skip()
  398. }
  399. }
  400. if err != nil {
  401. return err
  402. }
  403. i, err := strconv.ParseInt(string(buffer), 16, 32)
  404. if err != nil {
  405. return err
  406. }
  407. lexer.appendRune(rune(i))
  408. _, err = lexer.Peek()
  409. return err
  410. }
  411. func (lexer *Lexer) handleEscape() error {
  412. r, err := lexer.Skip()
  413. if err != nil {
  414. return err
  415. }
  416. switch r {
  417. case 'a':
  418. lexer.appendRune('\a')
  419. case 'b':
  420. lexer.appendRune('\b')
  421. case 'e':
  422. lexer.appendRune('\033')
  423. case 'f':
  424. lexer.appendRune('\f')
  425. case 'n':
  426. lexer.appendRune('\n')
  427. case 'r':
  428. lexer.appendRune('\r')
  429. case 't':
  430. lexer.appendRune('\t')
  431. case '\\':
  432. lexer.appendRune('\\')
  433. case '"':
  434. lexer.appendRune('"')
  435. // case 'o': fallthrough // No octals, for now.
  436. case 'x':
  437. err = lexer.handleEscapeHexChars(2)
  438. case 'u':
  439. err = lexer.handleEscapeHexChars(4)
  440. case 'U':
  441. err = lexer.handleEscapeHexChars(6)
  442. default:
  443. return fmt.Errorf("Unknown escape sequence character %c: %d", r, r)
  444. }
  445. return err
  446. }
  447. func (lexer *Lexer) LexString() Token {
  448. var err error
  449. var r rune
  450. _, err = lexer.Skip() // Skip first "
  451. if err != nil {
  452. return lexer.handleError(err)
  453. }
  454. r, err = lexer.Skip()
  455. for r != '"' && err == nil {
  456. if r == '\\' {
  457. err = lexer.handleEscape()
  458. if err != nil {
  459. return lexer.handleError(err)
  460. }
  461. } else {
  462. lexer.appendRune(r)
  463. // still inside the string
  464. }
  465. r, err = lexer.Skip()
  466. }
  467. if err != nil {
  468. return lexer.MakeErrorfToken("when parsing string: %s", err)
  469. }
  470. if err != nil {
  471. return lexer.handleError(err)
  472. }
  473. return lexer.MakeToken(TokenKindString)
  474. }
  475. func (lexer *Lexer) LexLongString() Token {
  476. var err error
  477. _, err = lexer.Skip()
  478. if err != nil {
  479. return lexer.handleError(err)
  480. }
  481. _, err = lexer.NextWhile(func(r rune) bool {
  482. return r != '`'
  483. })
  484. if err != nil {
  485. return lexer.MakeErrorfToken("when parsing long string: %s", err)
  486. }
  487. _, err = lexer.Skip()
  488. if err != nil {
  489. return lexer.handleError(err)
  490. }
  491. return lexer.MakeToken(TokenKindString)
  492. }
  493. func (lexer *Lexer) LexWordOrType(kind TokenKind) Token {
  494. var err error
  495. first := true
  496. _, err = lexer.Next()
  497. if err != nil {
  498. return lexer.handleError(err)
  499. }
  500. _, err = lexer.NextWhile(func(r rune) bool {
  501. if first {
  502. first = false
  503. return unicode.IsLetter(r) || r == '_'
  504. } else {
  505. return unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_'
  506. }
  507. })
  508. if err != nil {
  509. return lexer.handleError(err)
  510. }
  511. sbuffer := string(lexer.buffer)
  512. // handle keywords
  513. if kw, ok := lexer.Keywords[sbuffer] ; ok {
  514. return lexer.MakeTokenFromKeyword(kw)
  515. }
  516. return lexer.MakeToken(kind)
  517. }
  518. func (lexer *Lexer) LexWord() Token {
  519. return lexer.LexWordOrType(TokenKindWord)
  520. }
  521. func (lexer *Lexer) LexType() Token {
  522. return lexer.LexWordOrType(TokenKindType)
  523. }
  524. func (lexer *Lexer) LexSymbol() Token {
  525. var err error
  526. _, err = lexer.Skip()
  527. if err != nil {
  528. return lexer.handleError(err)
  529. }
  530. _, err = lexer.NextWhile(func(r rune) bool {
  531. return !isSpaceOrEOX(r)
  532. })
  533. if err != nil {
  534. return lexer.handleError(err)
  535. }
  536. return lexer.MakeToken(TokenKindSymbol)
  537. }
  538. func (lexer *Lexer) LexBuiltin() Token {
  539. var err error
  540. _, err = lexer.Skip()
  541. if err != nil {
  542. return lexer.handleError(err)
  543. }
  544. _, err = lexer.NextWhile(func(r rune) bool {
  545. return !isSpaceOrEOX(r)
  546. })
  547. if err != nil {
  548. return lexer.handleError(err)
  549. }
  550. return lexer.MakeBuiltinToken()
  551. }
  552. func (lexer *Lexer) skipSpaceAndCommentAndPeek() (rune, error) {
  553. r, err := lexer.Peek()
  554. if err != nil {
  555. return r, err
  556. }
  557. i := 0
  558. for isSpace(r) || isComment(r) {
  559. if isSpace(r) {
  560. err = lexer.SkipSpace()
  561. } else if isComment(r) {
  562. err = lexer.SkipComment()
  563. }
  564. if err != nil {
  565. return r, err
  566. }
  567. i++
  568. r, err = lexer.Peek()
  569. lexer.LogDebug("Peeked again: >%c< %v %v %d", r, isSpace(r), isComment(r), i)
  570. if err != nil {
  571. return r, err
  572. }
  573. }
  574. return r, err
  575. }
  576. func (lexer *Lexer) LexEOX() Token {
  577. lexer.Next()
  578. _, err := lexer.skipSpaceAndCommentAndPeek()
  579. if err != nil {
  580. return lexer.handleError(err)
  581. }
  582. _, err = lexer.NextWhile(func(r rune) bool {
  583. return isSpaceOrEOX(r) || r == '\n' || r == '.'
  584. })
  585. if err != nil {
  586. return lexer.handleError(err)
  587. }
  588. return lexer.MakeToken(TokenKindEOX)
  589. }
  590. func (lexer *Lexer) lex() Token {
  591. r, err := lexer.skipSpaceAndCommentAndPeek()
  592. lexer.LogDebug(" After skip: >%c< >%v<\n", r, err)
  593. if err != nil {
  594. return lexer.handleError(err)
  595. }
  596. if unicode.IsDigit(r) || r == '-' || r == '+' {
  597. return lexer.LexNumber()
  598. }
  599. if r == '\n' || r == '.' {
  600. return lexer.LexEOX()
  601. }
  602. if r == '"' {
  603. return lexer.LexString()
  604. }
  605. if r == '`' {
  606. return lexer.LexLongString()
  607. }
  608. if r == '!' {
  609. return lexer.LexBuiltin()
  610. }
  611. if r == ':' {
  612. return lexer.LexSymbol()
  613. }
  614. if isOperator(r) {
  615. return lexer.LexOperator()
  616. }
  617. switch TokenKind(r) {
  618. case TokenKindGet:
  619. fallthrough
  620. case TokenKindSet:
  621. fallthrough
  622. case TokenKindOpenBlock:
  623. fallthrough
  624. case TokenKindCloseBlock:
  625. fallthrough
  626. case TokenKindOpenList:
  627. fallthrough
  628. case TokenKindCloseList:
  629. fallthrough
  630. case TokenKindOpenParen:
  631. fallthrough
  632. case TokenKindCloseParen:
  633. lexer.Next()
  634. return lexer.MakeToken(TokenKind(r))
  635. default:
  636. }
  637. if unicode.IsLetter(r) || r == '_' {
  638. if unicode.IsUpper(r) {
  639. return lexer.LexType()
  640. } else {
  641. return lexer.LexWord()
  642. }
  643. }
  644. // EOF character
  645. if r == 0x7f {
  646. return lexer.MakeEOFToken()
  647. }
  648. return lexer.MakeErrorfToken("Unknown character: %c", r)
  649. }
  650. func (lexer *Lexer) Lex() Token {
  651. res := lexer.lex()
  652. lexer.ClearBuffer() // ensure buffer is cleared after lexing, always.
  653. return res
  654. }
  655. func (lexer *Lexer) LexAll() []Token {
  656. var token Token
  657. res := make([]Token, 0)
  658. for token = lexer.Lex(); !token.IsLast(); token = lexer.Lex() {
  659. res = append(res, token)
  660. }
  661. res = append(res, token)
  662. return res
  663. }
  664. func NewLexer(scanner io.RuneScanner, filename string) *Lexer {
  665. lexer := &Lexer{}
  666. lexer.RuneScanner = scanner
  667. lexer.Position.FileName = filename
  668. lexer.Position.Column = 1
  669. lexer.Position.Line = 1
  670. lexer.LoggerWrapper = LoggerWrapper{nil}
  671. lexer.Keywords = make(map[string]*Keyword)
  672. return lexer
  673. }
  674. func (lexer * Lexer) Report() {
  675. if lexer == nil {
  676. fmt.Printf("Lexer: is nil\n")
  677. } else {
  678. fmt.Printf("Lexer: %s:%d:%d\n",
  679. lexer.Position.FileName,
  680. lexer.Position.Column,
  681. lexer.Position.Line)
  682. }
  683. }
  684. func (lexer *Lexer) AddKeyword(kw * Keyword) *Keyword {
  685. if kw != nil {
  686. lexer.Keywords[kw.Name] = kw
  687. }
  688. return kw
  689. }
  690. func (lexer *Lexer) NewKeyword(name string, kind TokenKind, value Value) *Keyword {
  691. kw := &Keyword{Name: name, TokenKind: kind, Value: value}
  692. return lexer.AddKeyword(kw)
  693. }
  694. func NewLexerFromString(input string) *Lexer {
  695. reader := strings.NewReader(input)
  696. return NewLexer(reader, "<input>")
  697. }
  698. func NewLexerFromFilename(filename string) (*Lexer, error) {
  699. read, err := os.Open(filename)
  700. if err == nil {
  701. bread := bufio.NewReader(read)
  702. lex := NewLexer(bread, filename)
  703. return lex, nil
  704. }
  705. return nil, err
  706. }