1
1
mirror of https://github.com/github/semantic.git synced 2025-01-02 20:41:38 +03:00

Remember parentheses in syntax trees.

Adding this wasn't particularly hard. Just need to ensure we don't
throw the tokens from the parser away.

To test, evaluate `5 * (2 / 3)` versus `5 * 2 / 3`.
This commit is contained in:
Patrick Thomson 2018-04-06 11:43:48 -04:00
parent d20a0c554a
commit 611b0f3276
5 changed files with 19 additions and 4 deletions

View File

@ -146,6 +146,17 @@ instance Show1 Empty where liftShowsPrec _ _ _ _ = showString "Empty"
instance Evaluatable Empty where
eval _ = unit
-- | A parenthesized expression or statement. All the languages we target support this concept.
newtype Paren a = Paren a
deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable, FreeVariables1)
instance Eq1 Paren where liftEq = genericLiftEq
instance Ord1 Paren where liftCompare = genericLiftCompare
instance Show1 Paren where liftShowsPrec = genericLiftShowsPrec
instance Evaluatable Paren where
eval (Paren a) = subtermValue a
-- | Syntax representing a parsing or assignment error.
data Error a = Error { errorCallStack :: ErrorStack, errorExpected :: [String], errorActual :: Maybe String, errorChildren :: [a] }

View File

@ -89,6 +89,7 @@ type Syntax =
, Syntax.Error
, Syntax.Empty
, Syntax.Identifier
, Syntax.Paren
, Syntax.Program
, Type.Annotation
, Type.Array
@ -428,7 +429,7 @@ parameterDeclaration :: Assignment
parameterDeclaration = makeTerm <$> symbol ParameterDeclaration <*> children (manyTerm expression)
parenthesizedExpression :: Assignment
parenthesizedExpression = symbol ParenthesizedExpression *> children expressions
parenthesizedExpression = makeTerm <$> symbol ParenthesizedExpression <*> (Syntax.Paren <$> children expressions)
selectorExpression :: Assignment
selectorExpression = makeTerm <$> symbol SelectorExpression <*> children (Expression.MemberAccess <$> expression <*> expression)

View File

@ -103,6 +103,7 @@ type Syntax = '[
, Syntax.NamespaceUseDeclaration
, Syntax.NamespaceUseGroupClause
, Syntax.NewVariable
, Syntax.Paren
, Syntax.PrintIntrinsic
, Syntax.Program
, Syntax.PropertyDeclaration
@ -287,7 +288,7 @@ primaryExpression = choice [
]
parenthesizedExpression :: Assignment
parenthesizedExpression = symbol ParenthesizedExpression *> children (term expression)
parenthesizedExpression = makeTerm <$> symbol ParenthesizedExpression <*> (Syntax.Paren <$> children (term expression))
classConstantAccessExpression :: Assignment
classConstantAccessExpression = makeTerm <$> symbol ClassConstantAccessExpression <*> children (Expression.MemberAccess <$> term scopeResolutionQualifier <*> term name)

View File

@ -79,6 +79,7 @@ type Syntax =
, Syntax.Empty
, Syntax.Error
, Syntax.Identifier
, Syntax.Paren
, Syntax.Program
, Type.Annotation
, []
@ -190,7 +191,7 @@ keywordArgument :: Assignment
keywordArgument = makeTerm <$> symbol KeywordArgument <*> children (Statement.Assignment [] <$> term expression <*> term expression)
parenthesizedExpression :: Assignment
parenthesizedExpression = symbol ParenthesizedExpression *> children expressions
parenthesizedExpression = makeTerm <$> symbol ParenthesizedExpression <*> (Syntax.Paren <$> children expressions)
parameter :: Assignment
parameter = makeTerm <$> symbol DefaultParameter <*> children (Statement.Assignment [] <$> term expression <*> term expression)

View File

@ -86,6 +86,7 @@ type Syntax = '[
, Syntax.Empty
, Syntax.Error
, Syntax.Identifier
, Syntax.Paren
, Syntax.Program
, Syntax.Context
, Type.Readonly
@ -783,7 +784,7 @@ variableDeclarator = makeVarDecl <$> symbol VariableDeclarator <*> children ((,,
where makeVarDecl loc (subject, annotations, value) = makeTerm loc (Statement.Assignment [annotations] subject value)
parenthesizedExpression :: Assignment
parenthesizedExpression = symbol ParenthesizedExpression *> children (term expressions)
parenthesizedExpression = makeTerm <$> symbol ParenthesizedExpression <*> (Syntax.Paren <$> children (term expressions))
switchStatement :: Assignment
switchStatement = makeTerm <$> symbol SwitchStatement <*> children (Statement.Match <$> term parenthesizedExpression <*> term switchBody)