|
| 1 | +-- ? LPeg lexer. |
| 2 | + |
| 3 | +local l = require('lexer') |
| 4 | +local token, word_match = l.token, l.word_match |
| 5 | +local P, R, S = lpeg.P, lpeg.R, lpeg.S |
| 6 | + |
| 7 | +local M = {_NAME = 'hush'} |
| 8 | + |
| 9 | +local comment = token(l.COMMENT, '#' * l.nonnewline_esc^0) |
| 10 | + |
| 11 | +local constant = token(l.CONSTANT, word_match{ |
| 12 | + 'true', 'false', |
| 13 | +}) |
| 14 | + |
| 15 | +local identifier = token(l.IDENTIFIER, l.word) |
| 16 | + |
| 17 | +local keyword = token(l.KEYWORD, word_match{ |
| 18 | + 'let', 'if', 'then', 'else', 'elseif', 'end', 'for', 'in', 'do', 'while', |
| 19 | + 'function', 'return', 'not', 'and', 'or', 'true', 'false', 'nil', 'break', |
| 20 | + 'self', |
| 21 | +}) |
| 22 | + |
| 23 | +local operator = token(l.OPERATOR, word_match{ |
| 24 | + 'and', 'or', 'not', |
| 25 | +} + S('+-/*%<>!=[]')) |
| 26 | + |
| 27 | +local number = token(l.NUMBER, l.float + l.integer) |
| 28 | + |
| 29 | +local sq_str = l.delimited_range("'", true) |
| 30 | +local dq_str = l.delimited_range('"', true) |
| 31 | +local string = token(l.STRING, sq_str + dq_str) |
| 32 | + |
| 33 | +local type = token(l.TYPE, word_match{ |
| 34 | + 'int', 'char', 'float', 'string', 'bool', 'array', 'dict', |
| 35 | +}) |
| 36 | + |
| 37 | +local ws = token(l.WHITESPACE, l.space^1) |
| 38 | + |
| 39 | +M._rules = { |
| 40 | + {'constant', constant}, |
| 41 | + {'comment', comment}, |
| 42 | + {'keyword', keyword}, |
| 43 | + {'number', number}, |
| 44 | + {'operator', operator}, |
| 45 | + {'string', string}, |
| 46 | + {'type', type}, |
| 47 | + {'whitespace', ws}, |
| 48 | + {'identifier', identifier}, |
| 49 | +} |
| 50 | + |
| 51 | +return M |
0 commit comments