Introduce a Parens AST (#189)

* Introduce a Parens AST
This commit is contained in:
Dave Cunningham 2018-02-16 13:01:14 -05:00 committed by GitHub
parent f46dea2835
commit 2ea01b24d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 58 additions and 35 deletions

View File

@ -520,6 +520,15 @@ type ObjectComp struct {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Parens represents parentheses
// ( e )
type Parens struct {
NodeBase
Inner Node
}
// ---------------------------------------------------------------------------
// Self represents the self keyword. // Self represents the self keyword.
type Self struct{ NodeBase } type Self struct{ NodeBase }

View File

@ -15797,7 +15797,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(67), Line: int(67),
Column: int(79), Column: int(80),
}, },
file: p1, file: p1,
}, },
@ -22348,7 +22348,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(102), Line: int(102),
Column: int(86), Column: int(87),
}, },
file: p1, file: p1,
}, },
@ -30534,7 +30534,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(162), Line: int(162),
Column: int(58), Column: int(59),
}, },
file: p1, file: p1,
}, },
@ -31033,7 +31033,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(164), Line: int(164),
Column: int(67), Column: int(68),
}, },
file: p1, file: p1,
}, },
@ -31722,7 +31722,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(166), Line: int(166),
Column: int(95), Column: int(96),
}, },
file: p1, file: p1,
}, },
@ -37243,7 +37243,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(193), Line: int(193),
Column: int(81), Column: int(82),
}, },
file: p1, file: p1,
}, },
@ -37954,7 +37954,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(195), Line: int(195),
Column: int(87), Column: int(88),
}, },
file: p1, file: p1,
}, },
@ -39367,7 +39367,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(201), Line: int(201),
Column: int(90), Column: int(91),
}, },
file: p1, file: p1,
}, },
@ -40078,7 +40078,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(203), Line: int(203),
Column: int(87), Column: int(88),
}, },
file: p1, file: p1,
}, },
@ -41512,7 +41512,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(209), Line: int(209),
Column: int(88), Column: int(89),
}, },
file: p1, file: p1,
}, },
@ -41955,7 +41955,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(211), Line: int(211),
Column: int(86), Column: int(87),
}, },
file: p1, file: p1,
}, },
@ -69708,7 +69708,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(439), Line: int(439),
Column: int(59), Column: int(60),
}, },
file: p1, file: p1,
}, },
@ -69737,7 +69737,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(439), Line: int(439),
Column: int(59), Column: int(60),
}, },
file: p1, file: p1,
}, },
@ -69913,7 +69913,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(439), Line: int(439),
Column: int(59), Column: int(60),
}, },
file: p1, file: p1,
}, },
@ -70867,7 +70867,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(442), Line: int(442),
Column: int(74), Column: int(75),
}, },
file: p1, file: p1,
}, },
@ -73709,7 +73709,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(461), Line: int(461),
Column: int(53), Column: int(54),
}, },
file: p1, file: p1,
}, },
@ -73732,7 +73732,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(460), Line: int(460),
Column: int(74), Column: int(75),
}, },
file: p1, file: p1,
}, },
@ -90882,7 +90882,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(576), Line: int(576),
Column: int(95), Column: int(96),
}, },
file: p1, file: p1,
}, },
@ -103665,7 +103665,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(694), Line: int(694),
Column: int(94), Column: int(95),
}, },
file: p1, file: p1,
}, },
@ -104109,7 +104109,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(696), Line: int(696),
Column: int(92), Column: int(93),
}, },
file: p1, file: p1,
}, },
@ -104553,7 +104553,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(698), Line: int(698),
Column: int(83), Column: int(84),
}, },
file: p1, file: p1,
}, },
@ -116822,7 +116822,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(770), Line: int(770),
Column: int(54), Column: int(55),
}, },
file: p1, file: p1,
}, },
@ -140302,7 +140302,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(907), Line: int(907),
Column: int(62), Column: int(63),
}, },
file: p1, file: p1,
}, },
@ -140327,7 +140327,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(907), Line: int(907),
Column: int(62), Column: int(63),
}, },
file: p1, file: p1,
}, },
@ -142156,7 +142156,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(923), Line: int(923),
Column: int(87), Column: int(88),
}, },
file: p1, file: p1,
}, },
@ -142838,7 +142838,7 @@ var StdAst = &DesugaredObject{
}, },
End: Location{ End: Location{
Line: int(927), Line: int(927),
Column: int(96), Column: int(97),
}, },
file: p1, file: p1,
}, },

View File

@ -560,6 +560,13 @@ func desugar(astPtr *ast.Node, objLevel int) (err error) {
} }
*astPtr = comp *astPtr = comp
case *ast.Parens:
*astPtr = node.Inner
err = desugar(astPtr, objLevel)
if err != nil {
return err
}
case *ast.Self: case *ast.Self:
// Nothing to do. // Nothing to do.

View File

@ -99,6 +99,8 @@ func directChildren(node ast.Node) []ast.Node {
spec = spec.Outer spec = spec.Outer
} }
return result return result
case *ast.Parens:
return []ast.Node{node.Inner}
case *ast.Self: case *ast.Self:
return nil return nil
case *ast.SuperIndex: case *ast.SuperIndex:
@ -172,6 +174,8 @@ func thunkChildren(node ast.Node) []ast.Node {
return []ast.Node{node.Body} return []ast.Node{node.Body}
case *ast.ObjectComp: case *ast.ObjectComp:
return nil return nil
case *ast.Parens:
return nil
case *ast.Self: case *ast.Self:
return nil return nil
case *ast.SuperIndex: case *ast.SuperIndex:

View File

@ -724,11 +724,14 @@ func (p *parser) parseTerminal() (ast.Node, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, err = p.popExpect(tokenParenR) tokRight, err := p.popExpect(tokenParenR)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return inner, nil return &ast.Parens{
NodeBase: ast.NewNodeBaseLoc(locFromTokens(tok, tokRight)),
Inner: inner,
}, nil
// Literals // Literals
case tokenNumber: case tokenNumber:

View File

@ -229,9 +229,9 @@ var errorTests = []testError{
{`function a a`, `test:1:10-11 Expected ( but got (IDENTIFIER, "a")`}, {`function a a`, `test:1:10-11 Expected ( but got (IDENTIFIER, "a")`},
{`import (a b)`, `test:1:11-12 Expected token ")" but got (IDENTIFIER, "b")`}, {`import (a b)`, `test:1:11-12 Expected token ")" but got (IDENTIFIER, "b")`},
{`import (a+b)`, `test:1:9-12 Computed imports are not allowed`}, {`import (a+b)`, `test:1:8-13 Computed imports are not allowed`},
{`importstr (a b)`, `test:1:14-15 Expected token ")" but got (IDENTIFIER, "b")`}, {`importstr (a b)`, `test:1:14-15 Expected token ")" but got (IDENTIFIER, "b")`},
{`importstr (a+b)`, `test:1:12-15 Computed imports are not allowed`}, {`importstr (a+b)`, `test:1:11-16 Computed imports are not allowed`},
{`local a = b ()`, `test:1:15 Expected , or ; but got end of file`}, {`local a = b ()`, `test:1:15 Expected , or ; but got end of file`},
{`local a = b; (a b)`, `test:1:17-18 Expected token ")" but got (IDENTIFIER, "b")`}, {`local a = b; (a b)`, `test:1:17-18 Expected token ")" but got (IDENTIFIER, "b")`},

View File

@ -1,6 +1,6 @@
RUNTIME ERROR: Overflow RUNTIME ERROR: Overflow
------------------------------------------------- -------------------------------------------------
testdata/div4:1:1-19 $ testdata/div4:1:1-20 $
1/(1e-160)/(1e-160) 1/(1e-160)/(1e-160)

View File

@ -1,6 +1,6 @@
RUNTIME ERROR: Couldn't manifest function in JSON output. RUNTIME ERROR: Couldn't manifest function in JSON output.
------------------------------------------------- -------------------------------------------------
testdata/error_function_fail:1:1-22 $ testdata/error_function_fail:1:1-23 $
error (function(x) 42) error (function(x) 42)

View File

@ -1,6 +1,6 @@
RUNTIME ERROR: Too many values to format: 1, expected 0 RUNTIME ERROR: Too many values to format: 1, expected 0
------------------------------------------------- -------------------------------------------------
<std>:576:21-95 function <format_codes_arr> <std>:576:21-96 function <format_codes_arr>
error ("Too many values to format: " + std.length(arr) + ", expected " + j) error ("Too many values to format: " + std.length(arr) + ", expected " + j)

View File

@ -1,6 +1,6 @@
RUNTIME ERROR: Too many values to format: 2, expected 1 RUNTIME ERROR: Too many values to format: 2, expected 1
------------------------------------------------- -------------------------------------------------
<std>:576:21-95 function <format_codes_arr> <std>:576:21-96 function <format_codes_arr>
error ("Too many values to format: " + std.length(arr) + ", expected " + j) error ("Too many values to format: " + std.length(arr) + ", expected " + j)

View File

@ -1,6 +1,6 @@
RUNTIME ERROR: Couldn't manifest function in JSON output. RUNTIME ERROR: Couldn't manifest function in JSON output.
------------------------------------------------- -------------------------------------------------
testdata/string_plus_function:1:1-23 $ testdata/string_plus_function:1:1-24 $
"xxx" + (function() 42) "xxx" + (function() 42)