viml/parser/expressions: Add support for the dot operator and numbers

This commit is contained in:
ZyX
2017-10-03 01:30:02 +03:00
parent 163792e9b9
commit 21a5ce033c
3 changed files with 416 additions and 9 deletions

View File

@@ -77,6 +77,7 @@ make_enum_conv_tab(lib, {
'kExprNodeNested',
'kExprNodeCall',
'kExprNodePlainIdentifier',
'kExprNodePlainKey',
'kExprNodeComplexIdentifier',
'kExprNodeUnknownFigure',
'kExprNodeLambda',
@@ -86,6 +87,10 @@ make_enum_conv_tab(lib, {
'kExprNodeColon',
'kExprNodeArrow',
'kExprNodeComparison',
'kExprNodeConcat',
'kExprNodeConcatOrSubscript',
'kExprNodeInteger',
'kExprNodeFloat',
}, 'kExprNode', function(ret) east_node_type_tab = ret end)
local function conv_east_node_type(typ)
@@ -118,6 +123,9 @@ local function eastnode2lua(pstate, eastnode, checked_nodes)
typ = typ .. ('(scope=%s,ident=%s)'):format(
tostring(intchar2lua(eastnode.data.var.scope)),
ffi.string(eastnode.data.var.ident, eastnode.data.var.ident_len))
elseif typ == 'PlainKey' then
typ = typ .. ('(key=%s)'):format(
ffi.string(eastnode.data.var.ident, eastnode.data.var.ident_len))
elseif (typ == 'UnknownFigure' or typ == 'DictLiteral'
or typ == 'CurlyBracesIdentifier' or typ == 'Lambda') then
typ = typ .. ('(%s)'):format(
@@ -128,6 +136,10 @@ local function eastnode2lua(pstate, eastnode, checked_nodes)
typ = typ .. ('(type=%s,inv=%u,ccs=%s)'):format(
conv_cmp_type(eastnode.data.cmp.type), eastnode.data.cmp.inv and 1 or 0,
conv_ccs(eastnode.data.cmp.ccs))
elseif typ == 'Integer' then
typ = typ .. ('(val=%u)'):format(tonumber(eastnode.data.num.value))
elseif typ == 'Float' then
typ = typ .. ('(val=%e)'):format(tonumber(eastnode.data.flt.value))
end
ret_str = typ .. ':' .. ret_str
local can_simplify = true
@@ -190,6 +202,8 @@ end)
describe('Expressions parser', function()
local function check_parsing(str, flags, exp_ast, exp_highlighting_fs)
flags = flags or 0
local pstate = new_pstate({str})
local east = lib.viml_pexpr_parse(pstate, flags)
local ast = east2lua(pstate, east)
@@ -3649,4 +3663,284 @@ describe('Expressions parser', function()
hl('Identifier', 'b', 1),
})
end)
itp('works with concat/subscript', function()
check_parsing('.', 0, {
-- 0
ast = {
{
'ConcatOrSubscript:0:0:.',
children = {
'Missing:0:0:',
},
},
},
err = {
arg = '.',
msg = 'E15: Unexpected dot: %.*s',
},
}, {
hl('InvalidConcatOrSubscript', '.'),
})
check_parsing('a.', 0, {
-- 01
ast = {
{
'ConcatOrSubscript:0:1:.',
children = {
'PlainIdentifier(scope=0,ident=a):0:0:a',
},
},
},
err = {
arg = '',
msg = 'E15: Expected value, got EOC: %.*s',
},
}, {
hl('Identifier', 'a'),
hl('ConcatOrSubscript', '.'),
})
check_parsing('a.b', 0, {
-- 012
ast = {
{
'ConcatOrSubscript:0:1:.',
children = {
'PlainIdentifier(scope=0,ident=a):0:0:a',
'PlainKey(key=b):0:2:b',
},
},
},
}, {
hl('Identifier', 'a'),
hl('ConcatOrSubscript', '.'),
hl('IdentifierKey', 'b'),
})
check_parsing('1.2', 0, {
-- 012
ast = {
'Float(val=1.200000e+00):0:0:1.2',
},
}, {
hl('Float', '1.2'),
})
check_parsing('1.2 + 1.3e-5', 0, {
-- 012345678901
-- 0 1
ast = {
{
'BinaryPlus:0:3: +',
children = {
'Float(val=1.200000e+00):0:0:1.2',
'Float(val=1.300000e-05):0:5: 1.3e-5',
},
},
},
}, {
hl('Float', '1.2'),
hl('BinaryPlus', '+', 1),
hl('Float', '1.3e-5', 1),
})
check_parsing('a . 1.2 + 1.3e-5', 0, {
-- 0123456789012345
-- 0 1
ast = {
{
'BinaryPlus:0:7: +',
children = {
{
'Concat:0:1: .',
children = {
'PlainIdentifier(scope=0,ident=a):0:0:a',
{
'ConcatOrSubscript:0:5:.',
children = {
'Integer(val=1):0:3: 1',
'PlainKey(key=2):0:6:2',
},
},
},
},
'Float(val=1.300000e-05):0:9: 1.3e-5',
},
},
},
}, {
hl('Identifier', 'a'),
hl('Concat', '.', 1),
hl('Number', '1', 1),
hl('ConcatOrSubscript', '.'),
hl('IdentifierKey', '2'),
hl('BinaryPlus', '+', 1),
hl('Float', '1.3e-5', 1),
})
check_parsing('1.3e-5 + 1.2 . a', 0, {
-- 0123456789012345
-- 0 1
ast = {
{
'Concat:0:12: .',
children = {
{
'BinaryPlus:0:6: +',
children = {
'Float(val=1.300000e-05):0:0:1.3e-5',
'Float(val=1.200000e+00):0:8: 1.2',
},
},
'PlainIdentifier(scope=0,ident=a):0:14: a',
},
},
},
}, {
hl('Float', '1.3e-5'),
hl('BinaryPlus', '+', 1),
hl('Float', '1.2', 1),
hl('Concat', '.', 1),
hl('Identifier', 'a', 1),
})
check_parsing('1.3e-5 + a . 1.2', 0, {
-- 0123456789012345
-- 0 1
ast = {
{
'Concat:0:10: .',
children = {
{
'BinaryPlus:0:6: +',
children = {
'Float(val=1.300000e-05):0:0:1.3e-5',
'PlainIdentifier(scope=0,ident=a):0:8: a',
},
},
{
'ConcatOrSubscript:0:14:.',
children = {
'Integer(val=1):0:12: 1',
'PlainKey(key=2):0:15:2',
},
},
},
},
},
}, {
hl('Float', '1.3e-5'),
hl('BinaryPlus', '+', 1),
hl('Identifier', 'a', 1),
hl('Concat', '.', 1),
hl('Number', '1', 1),
hl('ConcatOrSubscript', '.'),
hl('IdentifierKey', '2'),
})
check_parsing('1.2.3', 0, {
-- 01234
ast = {
{
'ConcatOrSubscript:0:3:.',
children = {
{
'ConcatOrSubscript:0:1:.',
children = {
'Integer(val=1):0:0:1',
'PlainKey(key=2):0:2:2',
},
},
'PlainKey(key=3):0:4:3',
},
},
},
}, {
hl('Number', '1'),
hl('ConcatOrSubscript', '.'),
hl('IdentifierKey', '2'),
hl('ConcatOrSubscript', '.'),
hl('IdentifierKey', '3'),
})
check_parsing('a.1.2', 0, {
-- 01234
ast = {
{
'ConcatOrSubscript:0:3:.',
children = {
{
'ConcatOrSubscript:0:1:.',
children = {
'PlainIdentifier(scope=0,ident=a):0:0:a',
'PlainKey(key=1):0:2:1',
},
},
'PlainKey(key=2):0:4:2',
},
},
},
}, {
hl('Identifier', 'a'),
hl('ConcatOrSubscript', '.'),
hl('IdentifierKey', '1'),
hl('ConcatOrSubscript', '.'),
hl('IdentifierKey', '2'),
})
check_parsing('a . 1.2', 0, {
-- 0123456
ast = {
{
'Concat:0:1: .',
children = {
'PlainIdentifier(scope=0,ident=a):0:0:a',
{
'ConcatOrSubscript:0:5:.',
children = {
'Integer(val=1):0:3: 1',
'PlainKey(key=2):0:6:2',
},
},
},
},
},
}, {
hl('Identifier', 'a'),
hl('Concat', '.', 1),
hl('Number', '1', 1),
hl('ConcatOrSubscript', '.'),
hl('IdentifierKey', '2'),
})
check_parsing('+a . +b', 0, {
-- 0123456
ast = {
{
'Concat:0:2: .',
children = {
{
'UnaryPlus:0:0:+',
children = {
'PlainIdentifier(scope=0,ident=a):0:1:a',
},
},
{
'UnaryPlus:0:4: +',
children = {
'PlainIdentifier(scope=0,ident=b):0:6:b',
},
},
},
},
},
}, {
hl('UnaryPlus', '+'),
hl('Identifier', 'a'),
hl('Concat', '.', 1),
hl('UnaryPlus', '+', 1),
hl('Identifier', 'b'),
})
end)
end)