aboutsummaryrefslogtreecommitdiffhomepage
path: root/lexlua/yaml.lua
diff options
context:
space:
mode:
authormitchell <unknown>2020-04-25 16:26:31 -0400
committermitchell <unknown>2020-04-25 16:26:31 -0400
commitfad15f79b1230b3076be515d6894c8919562809b (patch)
tree72c848ef02c3331de5ca54eff7adaea3a9a6fb88 /lexlua/yaml.lua
parent1fd02a367dec125c0b49dd9246a0928433866b96 (diff)
downloadscintilla-mirror-fad15f79b1230b3076be515d6894c8919562809b.tar.gz
Reformatted Lua LPeg lexers and added new convenience functions and pattern.
`lexer.range()` replaces `lexer.delimited_range()` and `lexer.nested_pair()`. `lexer.to_eol()` replaces `patt * lexer.nonnewline^0` constructs. `lexer.number` replaces `lexer.float + lexer.integer`. Also added unit tests for lexer functions.
Diffstat (limited to 'lexlua/yaml.lua')
-rw-r--r--lexlua/yaml.lua74
1 files changed, 37 insertions, 37 deletions
diff --git a/lexlua/yaml.lua b/lexlua/yaml.lua
index 2cd54d210..fd70182fc 100644
--- a/lexlua/yaml.lua
+++ b/lexlua/yaml.lua
@@ -10,15 +10,16 @@ local M = {_NAME = 'yaml'}
-- Whitespace.
local indent = #lexer.starts_line(S(' \t')) *
- (token(lexer.WHITESPACE, ' ') + token('indent_error', '\t'))^1
+ (token(lexer.WHITESPACE, ' ') + token('indent_error', '\t'))^1
local ws = token(lexer.WHITESPACE, S(' \t')^1 + lexer.newline^1)
-- Comments.
-local comment = token(lexer.COMMENT, '#' * lexer.nonnewline^0)
+local comment = token(lexer.COMMENT, lexer.to_eol('#'))
-- Strings.
-local string = token(lexer.STRING, lexer.delimited_range("'") +
- lexer.delimited_range('"'))
+local sq_str = lexer.range("'")
+local dq_str = lexer.range('"')
+local string = token(lexer.STRING, sq_str + dq_str)
-- Numbers.
local integer = lexer.dec_num + lexer.hex_num + '0' * S('oO') * R('07')^1
@@ -26,22 +27,23 @@ local special_num = '.' * word_match({'inf', 'nan'}, nil, true)
local number = token(lexer.NUMBER, special_num + lexer.float + integer)
-- Timestamps.
-local ts = token('timestamp',
- lexer.digit * lexer.digit * lexer.digit * lexer.digit * -- year
- '-' * lexer.digit * lexer.digit^-1 * -- month
- '-' * lexer.digit * lexer.digit^-1 * -- day
- ((S(' \t')^1 + S('tT'))^-1 * -- separator
- lexer.digit * lexer.digit^-1 * -- hour
- ':' * lexer.digit * lexer.digit * -- minute
- ':' * lexer.digit * lexer.digit * -- second
- ('.' * lexer.digit^0)^-1 * -- fraction
- ('Z' + -- timezone
- S(' \t')^0 * S('-+') * lexer.digit * lexer.digit^-1 *
- (':' * lexer.digit * lexer.digit)^-1)^-1)^-1)
+local year = lexer.digit * lexer.digit * lexer.digit * lexer.digit
+local month = lexer.digit * lexer.digit^-1
+local day = lexer.digit * lexer.digit^-1
+local date = year * '-' * month * '-' * day
+local hours = lexer.digit * lexer.digit^-1
+local minutes = lexer.digit * lexer.digit
+local seconds = lexer.digit * lexer.digit
+local fraction = '.' * lexer.digit^0
+local time = hours * ':' * minutes * ':' * seconds * fraction^-1
+local T = S(' \t')^1 + S('tT')
+local zone = 'Z' + S(' \t')^0 * S('-+') * hours * (':' * minutes)^-1
+local ts = token('timestamp', date * (T * time * zone^-1))
-- Constants.
-local constant = token(lexer.CONSTANT,
- word_match({'null', 'true', 'false'}, nil, true))
+local constant = token(lexer.CONSTANT, word_match({
+ 'null', 'true', 'false'
+}, nil, true))
-- Types.
local type = token(lexer.TYPE, '!!' * word_match({
@@ -50,38 +52,36 @@ local type = token(lexer.TYPE, '!!' * word_match({
-- Scalar types.
'binary', 'bool', 'float', 'int', 'merge', 'null', 'str', 'timestamp',
'value', 'yaml'
-}, nil, true) + '!' * lexer.delimited_range('<>'))
+}, nil, true) + '!' * lexer.range('<', '>', true))
-- Document boundaries.
local doc_bounds = token('document', lexer.starts_line(P('---') + '...'))
-- Directives
local directive = token('directive', lexer.starts_line('%') *
- lexer.nonnewline^1)
+ lexer.nonnewline^1)
local word = (lexer.alpha + '-' * -lexer.space) * (lexer.alnum + '-')^0
-- Keys and literals.
local colon = S(' \t')^0 * ':' * (lexer.space + -1)
-local key = token(lexer.KEYWORD,
- #word * (lexer.nonnewline - colon)^1 * #colon *
- P(function(input, index)
- local line = input:sub(1, index - 1):match('[^\r\n]+$')
- return not line:find('[%w-]+:') and index
- end))
+local key = token(lexer.KEYWORD, #word * (lexer.nonnewline - colon)^1 * #colon *
+ P(function(input, index)
+ local line = input:sub(1, index - 1):match('[^\r\n]+$')
+ return not line:find('[%w-]+:') and index
+ end))
local value = #word * (lexer.nonnewline - lexer.space^0 * S(',]}'))^1
local block = S('|>') * S('+-')^-1 * (lexer.newline + -1) *
- function(input, index)
- local rest = input:sub(index)
- local level = #rest:match('^( *)')
- for pos, indent, line in rest:gmatch('() *()([^\r\n]+)') do
- if indent - pos < level and line ~= ' ' or
- level == 0 and pos > 1 then
- return index + pos - 1
- end
- end
- return #input + 1
- end
+ function(input, index)
+ local rest = input:sub(index)
+ local level = #rest:match('^( *)')
+ for pos, indent, line in rest:gmatch('() *()([^\r\n]+)') do
+ if indent - pos < level and line ~= ' ' or level == 0 and pos > 1 then
+ return index + pos - 1
+ end
+ end
+ return #input + 1
+ end
local literal = token('literal', value + block)
-- Indicators.