Skip to content

Commit d2ab471

Browse files
authored
Merge pull request #32 from juanjux/feature/annotations
Complete annotations for all nodes
2 parents 7763d96 + a372a05 commit d2ab471

25 files changed

+42880
-21393
lines changed

driver/main.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,5 @@ import (
77
)
88

99
func main() {
10-
d, err := driver.NewDriver(normalizer.ToNode, normalizer.Transformers)
11-
if err != nil {
12-
panic(err)
13-
}
14-
15-
s := driver.NewServer(d)
16-
if err := s.Start(); err != nil {
17-
panic(err)
18-
}
10+
driver.Run(normalizer.ToNode, normalizer.Transformers)
1911
}

driver/normalizer/annotation.go

Lines changed: 144 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,165 @@ var Transformers = []transformer.Tranformer{
2121

2222
var ErrRootMustBeFile = errors.NewKind("root must have internal type FILE")
2323

24+
var conditionOperators = On(intellij.ConditionOperator).Roles(uast.Operator).Children(
25+
On(HasToken("-eq")).Roles(uast.Relational, uast.Equal),
26+
On(HasToken("-ne")).Roles(uast.Relational, uast.Not, uast.Equal),
27+
On(HasToken("-gt")).Roles(uast.Relational, uast.GreaterThan),
28+
On(HasToken("-ge")).Roles(uast.Relational, uast.GreaterThanOrEqual),
29+
On(HasToken("-lt")).Roles(uast.Relational, uast.LessThan),
30+
On(HasToken("-le")).Roles(uast.Relational, uast.LessThanOrEqual),
31+
)
32+
33+
// XXX expressions/statements
34+
2435
// AnnotationRules describes how a UAST should be annotated with `uast.Role`.
2536
//
2637
// https://godoc.org/gopkg.in/bblfsh/sdk.v1/uast/ann
2738
var AnnotationRules = On(Any).Self(
2839
On(Not(intellij.File)).Error(ErrRootMustBeFile.New()),
2940
On(intellij.File).Roles(uast.File).Descendants(
30-
On(intellij.Comment).Roles(uast.Comment),
31-
On(intellij.Shebang).Roles(uast.Comment, uast.Documentation),
41+
On(intellij.Comment).Roles(uast.Comment, uast.Noop),
42+
On(intellij.LineFeed).Roles(uast.Whitespace, uast.Noop),
43+
On(intellij.Whitespace).Roles(uast.Whitespace, uast.Noop),
44+
On(intellij.IntLiteral).Roles(uast.Number, uast.Literal, uast.Primitive),
45+
46+
On(Or(intellij.VarUse, intellij.Variable)).Roles(uast.Expression, uast.Variable,
47+
uast.Identifier),
48+
On(Or(intellij.ComposedVariable, intellij.VarSubstitution)).Roles(uast.Variable,
49+
uast.Expression, uast.Identifier, uast.Incomplete),
50+
51+
On(intellij.UnEvalString).Roles(uast.Expression, uast.String, uast.Literal),
52+
On(intellij.StringBegin).Roles(uast.Expression, uast.String, uast.Block),
53+
On(intellij.StringEnd).Roles(uast.Expression, uast.String, uast.Incomplete),
54+
On(intellij.StringContent).Roles(uast.Expression, uast.String, uast.Literal),
55+
On(intellij.String).Roles(uast.Expression, uast.String, uast.Literal, uast.Block),
56+
// FIXME: needs uast node "Execute" or "Shell" or similar
57+
On(intellij.BackQuoteShellCommand).Roles(uast.Expression, uast.String, uast.Literal,
58+
uast.Call, uast.Incomplete),
59+
On(Or(intellij.SubShellCmd, intellij.PipelineCmd)).Roles(uast.Expression, uast.Call,
60+
uast.Incomplete),
61+
On(Or(intellij.Shebang, intellij.ShebangElement)).Roles(uast.Comment, uast.Pathname,
62+
uast.Incomplete),
3263
// variable declaration
33-
On(intellij.VarDefElement).Children(
34-
On(intellij.AssignmentWord).Roles(uast.Identifier),
64+
On(intellij.SimpleCommand).Roles(uast.Expression).Children(
65+
On(intellij.VarDefElement).Roles(uast.Expression, uast.Assignment, uast.Binary).Children(
66+
On(intellij.AssignmentWord).Roles(uast.Identifier, uast.Left),
67+
On(intellij.OperatorAssign).Roles(uast.Operator, uast.Assignment),
68+
On(And(Not(intellij.AssignmentWord), Not(intellij.OperatorAssign))).Roles(uast.Right),
69+
),
3570
),
3671
// function declaration
37-
On(intellij.FunctionDefElement).Children(
72+
On(intellij.FunctionDefElement).Roles(uast.Function, uast.Declaration, uast.Block).Children(
3873
On(intellij.Function).Roles(uast.Function, uast.Declaration),
3974
On(intellij.NamedSymbol).Roles(uast.Function, uast.Declaration, uast.Name),
4075
On(intellij.GroupElement).Roles(uast.Function, uast.Declaration, uast.Body, uast.Block),
4176
),
77+
// let statement / expression (unfortunately it doesnt produce a subtree...)
78+
On(intellij.LetExpression).Roles(uast.Expression, uast.Assignment, uast.Incomplete),
79+
On(intellij.LetStatement).Roles(uast.Statement, uast.Incomplete),
80+
4281
// if statement
43-
On(intellij.IfShellCommand).Roles(uast.If, uast.Statement),
82+
conditionOperators,
83+
On(intellij.ArithmeticCmd).Roles(uast.Expression, uast.Arithmetic, uast.Incomplete),
84+
On(intellij.ArithmeticSimple).Roles(uast.Expression, uast.Arithmetic, uast.Incomplete),
85+
On(Or(intellij.OperatorArithLess, intellij.OperatorLess)).Roles(uast.Operator, uast.Relational, uast.LessThan),
86+
On(Or(intellij.OperatorArithMore, intellij.OperatorMore)).Roles(uast.Operator, uast.Relational, uast.GreaterThan),
87+
On(intellij.OperatorArithEqual).Roles(uast.Operator, uast.Relational, uast.Equal),
88+
On(intellij.OperatorArithNotEqual).Roles(uast.Operator, uast.Relational, uast.Not, uast.Equal),
89+
On(intellij.OperatorNotEqual).Roles(uast.Operator, uast.Relational, uast.Not, uast.Equal),
90+
On(intellij.OperatorLessEqual).Roles(uast.Operator, uast.Relational, uast.LessThanOrEqual),
91+
On(intellij.OperatorMoreEqual).Roles(uast.Operator, uast.Relational, uast.GreaterThanOrEqual),
92+
On(intellij.OperatorBoolOr).Roles(uast.Operator, uast.Boolean, uast.Or),
93+
On(intellij.OperatorBoolAnd).Roles(uast.Operator, uast.Boolean, uast.And),
94+
On(intellij.OperatorBoolNot).Roles(uast.Operator, uast.Boolean, uast.Not),
95+
On(intellij.If).Roles(uast.Statement, uast.If),
96+
On(intellij.ConditionalShellCommand).Roles(uast.Expression, uast.Condition),
97+
On(intellij.IfShellCommand).Roles(uast.If, uast.Expression, uast.Block).Children(
98+
On(intellij.ElIf).Roles(uast.Statement, uast.If, uast.Else, uast.Incomplete),
99+
On(intellij.Else).Roles(uast.Statement, uast.Else, uast.Incomplete),
100+
On(intellij.SimpleCommand).Roles(uast.Expression, uast.If, uast.Condition),
101+
),
102+
On(intellij.Then).Roles(uast.Statement, uast.Incomplete),
103+
On(intellij.LogicalBlock).Roles(uast.Expression, uast.If, uast.Then),
104+
// FIXME: no role in the uast for "end"
105+
On(intellij.Fi).Roles(uast.Statement, uast.Incomplete),
44106
// for statement
45-
On(intellij.ForShellCommand).Roles(uast.For, uast.Statement),
107+
On(intellij.Do).Roles(uast.Statement, uast.Block),
108+
On(intellij.In).Roles(uast.Expression, uast.Binary, uast.Operator, uast.Relational, uast.Contains),
109+
On(intellij.GenericBash).Roles(uast.Incomplete).Children(
110+
On(HasToken("break")).Roles(uast.Statement, uast.Break),
111+
On(HasToken("continue")).Roles(uast.Statement, uast.Continue),
112+
),
113+
On(intellij.Done).Roles(uast.Statement, uast.Incomplete),
114+
115+
// These are more tokens that real semantic nodes, but they're in the AST tree
116+
// so we must tag them.
117+
On(intellij.BracketArithLeft).Roles(uast.Incomplete),
118+
On(intellij.BracketArithRight).Roles(uast.Incomplete),
119+
On(intellij.LeftConditional).Roles(uast.Incomplete),
120+
On(intellij.RightConditional).Roles(uast.Incomplete),
121+
On(intellij.SemiColon).Roles(uast.Incomplete),
122+
On(intellij.BraceOpen).Roles(uast.Incomplete),
123+
On(intellij.BraceClose).Roles(uast.Incomplete),
124+
On(intellij.SemiColon).Roles(uast.Incomplete),
125+
On(intellij.LeftSquare).Roles(uast.Incomplete),
126+
On(intellij.RightSquare).Roles(uast.Incomplete),
127+
On(intellij.DoubleSemiColon).Roles(uast.Incomplete),
128+
On(intellij.LeftBracket).Roles(uast.Incomplete),
129+
On(intellij.RightBracket).Roles(uast.Incomplete),
130+
On(intellij.ParenOpen).Roles(uast.Incomplete),
131+
On(intellij.DoubleParenOpen).Roles(uast.Incomplete),
132+
On(intellij.ParenClose).Roles(uast.Incomplete),
133+
On(intellij.DoubleParenClose).Roles(uast.Incomplete),
134+
On(intellij.BackQuote).Roles(uast.Incomplete),
135+
On(intellij.Dollar).Roles(uast.Incomplete),
136+
On(intellij.ErrorElement).Roles(uast.Incomplete),
137+
On(intellij.RedirectList).Roles(uast.Incomplete),
138+
On(intellij.RedirectElement).Roles(uast.Incomplete),
139+
140+
On(intellij.FileDescriptors).Roles(uast.Identifier, uast.Receiver, uast.Incomplete),
141+
On(intellij.Word).Roles(uast.Expression, uast.Identifier),
142+
On(intellij.CombinedWord).Roles(uast.Expression, uast.String, uast.Identifier, uast.Incomplete),
143+
On(intellij.ForShellCommand).Roles(uast.For, uast.Statement).Children(
144+
On(intellij.For).Roles(uast.Statement, uast.Incomplete),
145+
On(intellij.VarDefElement).Roles(uast.Expression, uast.For, uast.Iterator),
146+
On(intellij.LogicalBlock).Roles(uast.Expression, uast.For, uast.Body),
147+
On(intellij.CombinedWord).Roles(uast.Expression, uast.For, uast.Update),
148+
),
46149
// while and until statement
47-
On(intellij.WhileLoop).Roles(uast.While, uast.Statement),
48-
On(intellij.UntilLoop).Roles(uast.While, uast.Statement),
150+
On(Or(intellij.WhileLoop, intellij.While)).Roles(uast.Statement, uast.While),
151+
On(Or(intellij.UntilLoop, intellij.Until)).Roles(uast.Statement, uast.While, uast.Incomplete),
152+
On(Or(intellij.WhileLoop, intellij.UntilLoop)).Children(
153+
On(intellij.LogicalBlock).Roles(uast.Expression, uast.While, uast.Body),
154+
On(intellij.ConditionalShellCommand).Roles(uast.Expression, uast.While, uast.Condition).Children(
155+
On(intellij.LeftConditional).Roles(uast.Incomplete),
156+
On(intellij.RightConditional).Roles(uast.Incomplete),
157+
// "-a" and such
158+
On(intellij.ConditionOperator).Roles(uast.Operator, uast.Incomplete),
159+
),
160+
),
161+
On(intellij.OperatorEqual).Roles(uast.Expression, uast.Relational, uast.Operator, uast.Equal),
162+
On(intellij.OperatorBoolOr).Roles(uast.Expression, uast.Binary, uast.Operator, uast.Boolean, uast.Or),
163+
On(intellij.OperatorBoolAnd).Roles(uast.Expression, uast.Binary, uast.Operator, uast.Boolean, uast.And),
164+
On(intellij.ParameterExpOperatorA).Roles(uast.Operator, uast.Incomplete),
165+
On(intellij.ParameterExpOperatorH).Roles(uast.Operator, uast.Incomplete),
166+
On(intellij.ParameterExpOperatorP).Roles(uast.Operator, uast.Incomplete),
167+
On(intellij.ParameterExpOperatorC).Roles(uast.Operator, uast.Incomplete),
168+
On(intellij.ParameterExpOperatorS).Roles(uast.Operator, uast.Incomplete),
169+
On(intellij.AppendOperator).Roles(uast.Operator, uast.Incomplete),
170+
171+
// case pattern
172+
On(Or(intellij.Case, intellij.CasePattern2)).Roles(uast.Expression, uast.Case),
173+
On(intellij.CasePatternList).Roles(uast.Expression, uast.Case, uast.List).Children(
174+
On(intellij.LogicalBlock).Roles(uast.Expression, uast.Case, uast.Body),
175+
),
176+
On(intellij.CasePattern).Roles(uast.Statement, uast.Switch),
177+
On(intellij.CaseEnd).Roles(uast.Statement, uast.Incomplete),
178+
179+
// source command (import-ish)
180+
On(intellij.IncludeCommand).Roles(uast.Expression, uast.Import).Children(
181+
On(HasToken("source")).Roles(uast.Statement, uast.Import),
182+
On(intellij.SourceFileReference).Roles(uast.Expression, uast.Import, uast.Pathname, uast.Identifier),
183+
),
49184
),
50185
)

0 commit comments

Comments
 (0)