Skip to content

Commit a6d769d

Browse files
authored
Merge pull request #204 from cedricziel/fluid-arrays
[Fluid] Parse simple inline arrays
2 parents 5a62d32 + 487571d commit a6d769d

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

lang-fluid/src/main/grammars/FluidParser.bnf

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Expression ::= OrExpr
145145
| TernaryExpr
146146
| CastExpr
147147
| ViewHelperGroup
148+
| ArrayCreationExpr
148149
| FieldGroup
149150
| PrimaryGroup
150151

@@ -193,17 +194,15 @@ NumberExpr ::= NUMBER
193194

194195
ParenthesesExpr ::= '(' InlineChain ')' { pin=1 }
195196

196-
ArrayCreationExpr ::= InlineArray | NestedArray {
197-
pin=1
198-
}
199-
private InlineArray ::= ArrayElement ( ',' ArrayElement )* {
197+
ArrayCreationExpr ::= ArrayElement ( ',' ArrayElement )* {
200198
pin=1
201199
}
200+
202201
private NestedArray ::= '{' ArrayElement ( ',' ArrayElement )* '}' {
203202
pin=2
204203
}
205204

206-
ArrayElement ::= ArrayKey ':' ArrayValue {
205+
private ArrayElement ::= ArrayKey ':' ArrayValue {
207206
pin=2
208207
}
209208

lang-fluid/src/test/java/com/cedricziel/idea/fluid/lang/parser/ParserTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.cedricziel.idea.fluid.lang.psi.*;
44
import com.intellij.psi.PsiElement;
55
import com.intellij.psi.PsiFile;
6+
import com.intellij.psi.util.PsiTreeUtil;
67
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
78

89
public class ParserTest extends LightCodeInsightFixtureTestCase {
@@ -43,12 +44,36 @@ public void testCanDetectBooleanLiterals() {
4344
assertParentElementAtCaretMatchesType("{foo -> f:fo(foo: <caret>false)}", FluidBooleanLiteral.class);
4445
}
4546

47+
public void testCanParseArrays() {
48+
// Array Keys are discoverable
49+
assertParentElementAtCaretMatchesType("{fo<caret>o : bar}", FluidArrayKey.class);
50+
assertParentElementAtCaretMatchesType("{'fo<caret>o' : bar}", FluidArrayKey.class, 2);
51+
assertParentElementAtCaretMatchesType("{\"fo<caret>o\" : bar}", FluidArrayKey.class, 2);
52+
53+
// keys can be string literals
54+
assertParentElementAtCaretMatchesType("{'fo<caret>o' : bar}", FluidStringLiteral.class);
55+
assertParentElementAtCaretMatchesType("{\"fo<caret>o\" : bar}", FluidStringLiteral.class);
56+
57+
// array values can be literals or field chains
58+
assertParentElementAtCaretMatchesType("{foo : b<caret>ar}", FluidFieldChain.class);
59+
assertParentElementAtCaretMatchesType("{foo : b<caret>ar}", FluidArrayValue.class, 3);
60+
assertParentElementAtCaretMatchesType("{foo : 'b<caret>ar'}", FluidArrayValue.class, 2);
61+
assertParentElementAtCaretMatchesType("{foo : \"b<caret>ar\"}", FluidArrayValue.class, 2);
62+
}
63+
4664
private void assertParentElementAtCaretMatchesType(String content, Class expected) {
65+
assertParentElementAtCaretMatchesType(content, expected, 1);
66+
}
67+
68+
private void assertParentElementAtCaretMatchesType(String content, Class expected, int levelsUp) {
4769
PsiFile psiFile = myFixture.configureByText("foo.fluid", content);
4870
int offset = myFixture.getEditor().getCaretModel().getOffset();
4971

5072
PsiElement elementAt = psiFile.findElementAt(offset);
73+
for (int i = 0; i< levelsUp; i++) {
74+
elementAt = elementAt.getParent();
75+
}
5176

52-
assertInstanceOf(elementAt.getParent(), expected);
77+
assertInstanceOf(elementAt, expected);
5378
}
5479
}

0 commit comments

Comments
 (0)