mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
more xtend 2 java conversions
Signed-off-by: Christian Dietrich <christian.dietrich@itemis.de>
This commit is contained in:
parent
1ff1da2dfd
commit
b7a4664156
3 changed files with 290 additions and 829 deletions
|
@ -0,0 +1,290 @@
|
|||
/**
|
||||
* Copyright (c) 2017, 2021 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.parser.indentation;
|
||||
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtext.AbstractRule;
|
||||
import org.eclipse.xtext.Action;
|
||||
import org.eclipse.xtext.Keyword;
|
||||
import org.eclipse.xtext.RuleCall;
|
||||
import org.eclipse.xtext.nodemodel.ICompositeNode;
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode;
|
||||
import org.eclipse.xtext.nodemodel.INode;
|
||||
import org.eclipse.xtext.nodemodel.impl.InvariantChecker;
|
||||
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
|
||||
import org.eclipse.xtext.parser.indentation.indentationAwareTestLanguage.Tree;
|
||||
import org.eclipse.xtext.parser.indentation.tests.IndentationAwareTestLanguageInjectorProvider;
|
||||
import org.eclipse.xtext.testing.InjectWith;
|
||||
import org.eclipse.xtext.testing.XtextRunner;
|
||||
import org.eclipse.xtext.testing.util.ParseHelper;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* @author Sebastian Zarnekow - Initial contribution and API
|
||||
*/
|
||||
@RunWith(XtextRunner.class)
|
||||
@InjectWith(IndentationAwareTestLanguageInjectorProvider.class)
|
||||
public class NodeModelTest {
|
||||
public static void assertEquals(Object expected, Object actual) {
|
||||
Assert.assertEquals(expected.toString().replaceAll("\r\n", "\n"), actual.toString().replaceAll("\r\n", "\n"));
|
||||
}
|
||||
|
||||
@Inject
|
||||
private ParseHelper<Tree> parseHelper;
|
||||
|
||||
@Inject
|
||||
private InvariantChecker invariantChecker;
|
||||
|
||||
@Test
|
||||
public void testEmptyTree() throws Exception {
|
||||
ICompositeNode tree = getRootNode("");
|
||||
NodeModelTest.assertEquals("", asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleRootNode() throws Exception {
|
||||
ICompositeNode tree = getRootNode("root");
|
||||
String expectation = "[ID:root]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoRootNodes() throws Exception {
|
||||
String model =
|
||||
"first\n" +
|
||||
"second\n";
|
||||
ICompositeNode tree = getRootNode(model);
|
||||
String expectation =
|
||||
"[ID:first][-WS:\n" +
|
||||
"][ID:second][-WS:\n" +
|
||||
"]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParentChild() throws Exception {
|
||||
String model =
|
||||
"parent\n" +
|
||||
" child\n";
|
||||
ICompositeNode tree = getRootNode(model);
|
||||
String expectation =
|
||||
"[ID:parent][-WS:\n" +
|
||||
"\\t][INDENT:][ID:child][-WS:\n" +
|
||||
"][DEDENT:]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParentChildWithEof() throws Exception {
|
||||
String model =
|
||||
"parent\n" +
|
||||
" child\n";
|
||||
ICompositeNode tree = getRootNode(model.trim());
|
||||
String expectation = "[ID:parent][-WS:\n" +
|
||||
"\\t][INDENT:][ID:child][DEDENT:]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParentChildren() throws Exception {
|
||||
String model =
|
||||
"parent\n" +
|
||||
" child\n" +
|
||||
" child\n";
|
||||
ICompositeNode tree = getRootNode(model);
|
||||
String expectation =
|
||||
"[ID:parent][-WS:\n" +
|
||||
"\\t][INDENT:][ID:child][-WS:\n" +
|
||||
"\\t][ID:child][-WS:\n" +
|
||||
"][DEDENT:]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTree_01() throws Exception {
|
||||
String model =
|
||||
"a\n" +
|
||||
" b\n" +
|
||||
" c\n" +
|
||||
" d\n";
|
||||
ICompositeNode tree = getRootNode(model);
|
||||
String expectation =
|
||||
"[ID:a][-WS:\n" +
|
||||
"\\t][INDENT:][ID:b][-WS:\n" +
|
||||
"\\t ][INDENT:][ID:c][-WS:\n" +
|
||||
"][DEDENT:][-WS:\\t][ID:d][-WS:\n" +
|
||||
"][DEDENT:]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWeirdTree() throws Exception {
|
||||
String model =
|
||||
"root\n" +
|
||||
" s4\n" +
|
||||
" s8\n" +
|
||||
" s6\n";
|
||||
ICompositeNode tree = getRootNode(model);
|
||||
String expectation =
|
||||
"[ID:root][-WS:\n" +
|
||||
" ][INDENT:][ID:s4][-WS:\n" +
|
||||
" ][INDENT:][ID:s8][-WS:\n" +
|
||||
"][DEDENT:][-WS: ][INDENT:][ID:s6][-WS:\n" +
|
||||
"][DEDENT:][DEDENT:]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWeirdTreeEof() throws Exception {
|
||||
String model =
|
||||
"root\n" +
|
||||
" s4\n" +
|
||||
" s8\n" +
|
||||
" s6\n";
|
||||
ICompositeNode tree = getRootNode(model.trim());
|
||||
String expectation =
|
||||
"[ID:root][-WS:\n" +
|
||||
" ][INDENT:][ID:s4][-WS:\n" +
|
||||
" ][INDENT:][ID:s8][-WS:\n" +
|
||||
"][DEDENT:][-WS: ][INDENT:][ID:s6][DEDENT:][DEDENT:]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_1() throws Exception {
|
||||
String model =
|
||||
"first\n" +
|
||||
" \n" +
|
||||
"second\n";
|
||||
ICompositeNode tree = getRootNode(model);
|
||||
String expectation =
|
||||
"[ID:first][-WS:\n" +
|
||||
"\\t\n" +
|
||||
"][ID:second][-WS:\n" +
|
||||
"]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_2() throws Exception {
|
||||
String model =
|
||||
"\"first\"\n" +
|
||||
" \n" +
|
||||
"\"second\"\n";
|
||||
ICompositeNode tree = getRootNode(model);
|
||||
String expectation =
|
||||
"[STRING:\"first\"][-WS:\n" +
|
||||
"\\t\n" +
|
||||
"][STRING:\"second\"][-WS:\n" +
|
||||
"]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_3() throws Exception {
|
||||
ICompositeNode tree = getRootNode("first\n\t");
|
||||
String expectation =
|
||||
"[ID:first][-WS:\n" +
|
||||
"\\t]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_4() throws Exception {
|
||||
// first<LB>
|
||||
// abc<LB>
|
||||
// <EOF>
|
||||
ICompositeNode tree = getRootNode("first\n\t\tabc\n\t");
|
||||
String expectation =
|
||||
"[ID:first][-WS:\n" +
|
||||
"\\t\\t][INDENT:][ID:abc][-WS:\n" +
|
||||
"][DEDENT:][-WS:\\t]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_5() throws Exception {
|
||||
// first<LB>
|
||||
// abc<LB>
|
||||
// <LB>
|
||||
// <EOF>
|
||||
ICompositeNode tree = getRootNode("first\n\t\tabc\n\n");
|
||||
String expectation =
|
||||
"[ID:first][-WS:\n" +
|
||||
"\\t\\t][INDENT:][ID:abc][-WS:\n" +
|
||||
"][DEDENT:][-WS:\n" +
|
||||
"]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_6() throws Exception {
|
||||
// first<LB>
|
||||
// abc<LB>
|
||||
// <LB>
|
||||
// <EOF>
|
||||
ICompositeNode tree = getRootNode("first\n\t\tabc\n\t\n");
|
||||
String expectation =
|
||||
"[ID:first][-WS:\n" +
|
||||
"\\t\\t][INDENT:][ID:abc][-WS:\n" +
|
||||
"][DEDENT:][-WS:\\t\n" +
|
||||
"]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_7() throws Exception {
|
||||
// a<LB>
|
||||
// b<LB>
|
||||
// <EOF>
|
||||
ICompositeNode tree = getRootNode("a\n\tb\n\t ");
|
||||
String expectation =
|
||||
"[ID:a][-WS:\n" +
|
||||
"\\t][INDENT:][ID:b][-WS:\n" +
|
||||
"\\t ][DEDENT:]\n";
|
||||
NodeModelTest.assertEquals(expectation.trim(), asText(tree));
|
||||
}
|
||||
|
||||
private ICompositeNode getRootNode(CharSequence seq) throws Exception {
|
||||
Tree model = parseHelper.parse(seq.toString().replaceAll("\r\n", "\n"));
|
||||
ICompositeNode result = NodeModelUtils.getNode(model).getRootNode();
|
||||
invariantChecker.checkInvariant(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private String asText(INode node) {
|
||||
Iterable<ILeafNode> filtered = Iterables.filter(node.getLeafNodes(),
|
||||
(ILeafNode it) -> (!(it.getGrammarElement() instanceof Action)));
|
||||
|
||||
if (Iterables.isEmpty(filtered)) {
|
||||
return "";
|
||||
}
|
||||
return "[" + Joiner.on("][").join(Iterables.transform(filtered, (ILeafNode it) -> {
|
||||
return (it.isHidden() ? "-" : "") + tokenType(it.getGrammarElement()) + ":"
|
||||
+ it.getText().replace("\t", "\\t");
|
||||
})) + "]";
|
||||
}
|
||||
|
||||
private String tokenType(EObject obj) {
|
||||
if (obj instanceof RuleCall) {
|
||||
return ((RuleCall) obj).getRule().getName();
|
||||
} else if (obj instanceof Keyword) {
|
||||
return ((Keyword) obj).getValue();
|
||||
} else if (obj instanceof AbstractRule) {
|
||||
return ((AbstractRule) obj).getName();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,428 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.parser.indentation
|
||||
|
||||
import com.google.inject.Inject
|
||||
import org.eclipse.emf.ecore.EObject
|
||||
import org.eclipse.xtext.AbstractRule
|
||||
import org.eclipse.xtext.Action
|
||||
import org.eclipse.xtext.Keyword
|
||||
import org.eclipse.xtext.RuleCall
|
||||
import org.eclipse.xtext.nodemodel.INode
|
||||
import org.eclipse.xtext.nodemodel.impl.InvariantChecker
|
||||
import org.eclipse.xtext.nodemodel.util.NodeModelUtils
|
||||
import org.eclipse.xtext.parser.indentation.indentationAwareTestLanguage.Tree
|
||||
import org.eclipse.xtext.parser.indentation.tests.IndentationAwareTestLanguageInjectorProvider
|
||||
import org.eclipse.xtext.testing.InjectWith
|
||||
import org.eclipse.xtext.testing.XtextRunner
|
||||
import org.eclipse.xtext.testing.util.ParseHelper
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
/**
|
||||
* @author Sebastian Zarnekow - Initial contribution and API
|
||||
*/
|
||||
@RunWith(XtextRunner)
|
||||
@InjectWith(IndentationAwareTestLanguageInjectorProvider)
|
||||
class NodeModelTest {
|
||||
|
||||
def static void assertEquals(Object expected, Object actual) {
|
||||
Assert.assertEquals(expected.toString.replaceAll("\r\n","\n"), actual.toString.replaceAll("\r\n","\n"))
|
||||
}
|
||||
|
||||
@Inject ParseHelper<Tree> parseHelper
|
||||
|
||||
@Inject
|
||||
extension InvariantChecker invariantChecker
|
||||
|
||||
@Test
|
||||
def void testEmptyTree() {
|
||||
val tree = ''.rootNode
|
||||
''.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testSingleRootNode() {
|
||||
val tree = 'root'.rootNode
|
||||
'''
|
||||
[ID:root]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testTwoRootNodes() {
|
||||
val tree = '''
|
||||
first
|
||||
second
|
||||
'''.rootNode
|
||||
'''
|
||||
[ID:first][-WS:
|
||||
][ID:second][-WS:
|
||||
]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testParentChild() {
|
||||
val tree = '''
|
||||
parent
|
||||
child
|
||||
'''.rootNode
|
||||
'''
|
||||
[ID:parent][-WS:
|
||||
\t][INDENT:][ID:child][-WS:
|
||||
][DEDENT:]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testParentChildWithEof() {
|
||||
val tree = '''
|
||||
parent
|
||||
child
|
||||
'''.toString.trim.rootNode
|
||||
'''
|
||||
[ID:parent][-WS:
|
||||
\t][INDENT:][ID:child][DEDENT:]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testParentChildren() {
|
||||
val tree = '''
|
||||
parent
|
||||
child
|
||||
child
|
||||
'''.rootNode
|
||||
'''
|
||||
[ID:parent][-WS:
|
||||
\t][INDENT:][ID:child][-WS:
|
||||
\t][ID:child][-WS:
|
||||
][DEDENT:]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testTree_01() {
|
||||
val tree = '''
|
||||
a
|
||||
b
|
||||
c
|
||||
d
|
||||
'''.rootNode
|
||||
'''
|
||||
[ID:a][-WS:
|
||||
\t][INDENT:][ID:b][-WS:
|
||||
\t ][INDENT:][ID:c][-WS:
|
||||
][DEDENT:][-WS:\t][ID:d][-WS:
|
||||
][DEDENT:]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testWeirdTree() {
|
||||
val tree = '''
|
||||
root
|
||||
s4
|
||||
s8
|
||||
s6
|
||||
'''.rootNode
|
||||
'''
|
||||
[ID:root][-WS:
|
||||
][INDENT:][ID:s4][-WS:
|
||||
][INDENT:][ID:s8][-WS:
|
||||
][DEDENT:][-WS: ][INDENT:][ID:s6][-WS:
|
||||
][DEDENT:][DEDENT:]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testWeirdTreeEof() {
|
||||
val tree = '''
|
||||
root
|
||||
s4
|
||||
s8
|
||||
s6
|
||||
'''.toString.trim.rootNode
|
||||
'''
|
||||
[ID:root][-WS:
|
||||
][INDENT:][ID:s4][-WS:
|
||||
][INDENT:][ID:s8][-WS:
|
||||
][DEDENT:][-WS: ][INDENT:][ID:s6][DEDENT:][DEDENT:]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testIgnoreEmptyLines_1() {
|
||||
val tree = '''
|
||||
first
|
||||
|
||||
second
|
||||
'''.rootNode
|
||||
'''
|
||||
[ID:first][-WS:
|
||||
\t
|
||||
][ID:second][-WS:
|
||||
]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testIgnoreEmptyLines_2() {
|
||||
val tree = '''
|
||||
"first"
|
||||
|
||||
"second"
|
||||
'''.rootNode
|
||||
'''
|
||||
[STRING:"first"][-WS:
|
||||
\t
|
||||
][STRING:"second"][-WS:
|
||||
]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testIgnoreEmptyLines_3() {
|
||||
val tree = 'first\n\t'.rootNode
|
||||
'''
|
||||
[ID:first][-WS:
|
||||
\t]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testIgnoreEmptyLines_4() {
|
||||
// first<LB>
|
||||
// abc<LB>
|
||||
// <EOF>
|
||||
val tree = 'first\n\t\tabc\n\t'.rootNode
|
||||
'''
|
||||
[ID:first][-WS:
|
||||
\t\t][INDENT:][ID:abc][-WS:
|
||||
][DEDENT:][-WS:\t]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testIgnoreEmptyLines_5() {
|
||||
// first<LB>
|
||||
// abc<LB>
|
||||
// <LB>
|
||||
// <EOF>
|
||||
val tree = 'first\n\t\tabc\n\n'.rootNode
|
||||
'''
|
||||
[ID:first][-WS:
|
||||
\t\t][INDENT:][ID:abc][-WS:
|
||||
][DEDENT:][-WS:
|
||||
]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testIgnoreEmptyLines_6() {
|
||||
// first<LB>
|
||||
// abc<LB>
|
||||
// <LB>
|
||||
// <EOF>
|
||||
val tree = 'first\n\t\tabc\n\t\n'.rootNode
|
||||
'''
|
||||
[ID:first][-WS:
|
||||
\t\t][INDENT:][ID:abc][-WS:
|
||||
][DEDENT:][-WS:\t
|
||||
]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
@Test
|
||||
def void testIgnoreEmptyLines_7() {
|
||||
// a<LB>
|
||||
// b<LB>
|
||||
// <EOF>
|
||||
val tree = 'a\n\tb\n\t '.rootNode
|
||||
'''
|
||||
[ID:a][-WS:
|
||||
\t][INDENT:][ID:b][-WS:
|
||||
\t ][DEDENT:]
|
||||
'''.toString.trim.assertEquals(tree.asText)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// def void testTree_02() {
|
||||
// val tree = '''
|
||||
// a
|
||||
// b
|
||||
// c
|
||||
// d
|
||||
// e
|
||||
// f
|
||||
// g
|
||||
// h
|
||||
// '''.parseAndValidate
|
||||
// '''
|
||||
// a
|
||||
// b
|
||||
// c
|
||||
// d
|
||||
// e
|
||||
// f
|
||||
// g
|
||||
// h
|
||||
// '''.toString.assertEquals(tree.asText)
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// def void testTree_03() {
|
||||
// val tree = '''
|
||||
// a
|
||||
// b // single tab
|
||||
// c // 8 spaces eq 1 tab
|
||||
// d
|
||||
// '''.parseAndValidate
|
||||
// '''
|
||||
// a
|
||||
// b
|
||||
// c
|
||||
// d
|
||||
// '''.toString.assertEquals(tree.asText)
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// def void testTree_04() {
|
||||
// val tree = '''
|
||||
// level0_1
|
||||
// level1_1
|
||||
// level1_2
|
||||
// level2_1
|
||||
// level2_2
|
||||
// level1_3
|
||||
// level2_3
|
||||
// level2_4
|
||||
// level3_1
|
||||
// level3_2
|
||||
// level2_5
|
||||
// level1_4
|
||||
// level2_6
|
||||
// level0_2
|
||||
// level1_5
|
||||
// level2_7
|
||||
// level3_8
|
||||
// level4_9
|
||||
// '''.parseAndValidate
|
||||
// '''
|
||||
// level0_1
|
||||
// level1_1
|
||||
// level1_2
|
||||
// level2_1
|
||||
// level2_2
|
||||
// level1_3
|
||||
// level2_3
|
||||
// level2_4
|
||||
// level3_1
|
||||
// level3_2
|
||||
// level2_5
|
||||
// level1_4
|
||||
// level2_6
|
||||
// level0_2
|
||||
// level1_5
|
||||
// level2_7
|
||||
// level3_8
|
||||
// level4_9
|
||||
// '''.toString.assertEquals(tree.asText)
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// def void testTree_05() {
|
||||
// val tree = '''
|
||||
// a
|
||||
// x
|
||||
// b // two tabs
|
||||
// c // tab and 8 spaces (eq 2 tabs)
|
||||
// y
|
||||
// '''.parseAndValidate
|
||||
// '''
|
||||
// a
|
||||
// x
|
||||
// b
|
||||
// c
|
||||
// y
|
||||
// '''.toString.assertEquals(tree.asText)
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// def void testTree_06() {
|
||||
// val tree = '''
|
||||
// "a"
|
||||
// "b"
|
||||
// '''.parseAndValidate
|
||||
// '''
|
||||
// a
|
||||
// b
|
||||
// '''.toString.assertEquals(tree.asText)
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// def void testTree_07() {
|
||||
// val tree = '''
|
||||
// "a"
|
||||
//
|
||||
// "b"
|
||||
//
|
||||
// '''.parseAndValidate
|
||||
// '''
|
||||
// a
|
||||
// >
|
||||
// b
|
||||
// >
|
||||
// '''.toString.assertEquals(tree.asText)
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// def void testTree_08() {
|
||||
// val tree = '''
|
||||
// "a"
|
||||
// "1"
|
||||
// "2"
|
||||
// "b"
|
||||
// "3"
|
||||
// '''.parseAndValidate
|
||||
// '''
|
||||
// a
|
||||
// >
|
||||
// 1
|
||||
// 2
|
||||
// b
|
||||
// >
|
||||
// 3
|
||||
// '''.toString.assertEquals(tree.asText)
|
||||
// }
|
||||
|
||||
private def getRootNode(CharSequence seq) {
|
||||
val model = parseHelper.parse(seq.toString.replaceAll("\r\n","\n"));
|
||||
val result = NodeModelUtils.getNode(model).rootNode
|
||||
result.checkInvariant
|
||||
return result
|
||||
}
|
||||
|
||||
private def String asText(INode node) {
|
||||
node.leafNodes.filter[!(grammarElement instanceof Action)].join('[', '][', ']') [
|
||||
(if (isHidden) '-' else '') + grammarElement.tokenType + ':' + text.replace('\t', '\\t')
|
||||
]
|
||||
}
|
||||
|
||||
private def String tokenType(EObject obj) {
|
||||
switch(obj) {
|
||||
RuleCall: obj.rule.name
|
||||
Keyword: obj.value
|
||||
AbstractRule: obj.name
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,401 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2017 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.xtext.parser.indentation;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.xtend2.lib.StringConcatenation;
|
||||
import org.eclipse.xtext.AbstractRule;
|
||||
import org.eclipse.xtext.Action;
|
||||
import org.eclipse.xtext.Keyword;
|
||||
import org.eclipse.xtext.RuleCall;
|
||||
import org.eclipse.xtext.nodemodel.ICompositeNode;
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode;
|
||||
import org.eclipse.xtext.nodemodel.INode;
|
||||
import org.eclipse.xtext.nodemodel.impl.InvariantChecker;
|
||||
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
|
||||
import org.eclipse.xtext.parser.indentation.indentationAwareTestLanguage.Tree;
|
||||
import org.eclipse.xtext.parser.indentation.tests.IndentationAwareTestLanguageInjectorProvider;
|
||||
import org.eclipse.xtext.testing.InjectWith;
|
||||
import org.eclipse.xtext.testing.XtextRunner;
|
||||
import org.eclipse.xtext.testing.util.ParseHelper;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xbase.lib.Extension;
|
||||
import org.eclipse.xtext.xbase.lib.Functions.Function1;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* @author Sebastian Zarnekow - Initial contribution and API
|
||||
*/
|
||||
@RunWith(XtextRunner.class)
|
||||
@InjectWith(IndentationAwareTestLanguageInjectorProvider.class)
|
||||
@SuppressWarnings("all")
|
||||
public class NodeModelTest {
|
||||
public static void assertEquals(final Object expected, final Object actual) {
|
||||
Assert.assertEquals(expected.toString().replaceAll("\r\n", "\n"), actual.toString().replaceAll("\r\n", "\n"));
|
||||
}
|
||||
|
||||
@Inject
|
||||
private ParseHelper<Tree> parseHelper;
|
||||
|
||||
@Inject
|
||||
@Extension
|
||||
private InvariantChecker invariantChecker;
|
||||
|
||||
@Test
|
||||
public void testEmptyTree() {
|
||||
final ICompositeNode tree = this.getRootNode("");
|
||||
NodeModelTest.assertEquals("", this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleRootNode() {
|
||||
final ICompositeNode tree = this.getRootNode("root");
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("[ID:root]");
|
||||
_builder.newLine();
|
||||
NodeModelTest.assertEquals(_builder.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoRootNodes() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("first");
|
||||
_builder.newLine();
|
||||
_builder.append("second");
|
||||
_builder.newLine();
|
||||
final ICompositeNode tree = this.getRootNode(_builder);
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[ID:first][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("][ID:second][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("]");
|
||||
_builder_1.newLine();
|
||||
NodeModelTest.assertEquals(_builder_1.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParentChild() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("parent");
|
||||
_builder.newLine();
|
||||
_builder.append("\t");
|
||||
_builder.append("child");
|
||||
_builder.newLine();
|
||||
final ICompositeNode tree = this.getRootNode(_builder);
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[ID:parent][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("\\t][INDENT:][ID:child][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("][DEDENT:]");
|
||||
_builder_1.newLine();
|
||||
NodeModelTest.assertEquals(_builder_1.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParentChildWithEof() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("parent");
|
||||
_builder.newLine();
|
||||
_builder.append("\t");
|
||||
_builder.append("child");
|
||||
_builder.newLine();
|
||||
final ICompositeNode tree = this.getRootNode(_builder.toString().trim());
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[ID:parent][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("\\t][INDENT:][ID:child][DEDENT:]");
|
||||
_builder_1.newLine();
|
||||
NodeModelTest.assertEquals(_builder_1.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParentChildren() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("parent");
|
||||
_builder.newLine();
|
||||
_builder.append("\t");
|
||||
_builder.append("child");
|
||||
_builder.newLine();
|
||||
_builder.append("\t");
|
||||
_builder.append("child");
|
||||
_builder.newLine();
|
||||
final ICompositeNode tree = this.getRootNode(_builder);
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[ID:parent][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("\\t][INDENT:][ID:child][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("\\t][ID:child][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("][DEDENT:]");
|
||||
_builder_1.newLine();
|
||||
NodeModelTest.assertEquals(_builder_1.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTree_01() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("a");
|
||||
_builder.newLine();
|
||||
_builder.append("\t");
|
||||
_builder.append("b");
|
||||
_builder.newLine();
|
||||
_builder.append("\t ");
|
||||
_builder.append("c");
|
||||
_builder.newLine();
|
||||
_builder.append("\t");
|
||||
_builder.append("d");
|
||||
_builder.newLine();
|
||||
final ICompositeNode tree = this.getRootNode(_builder);
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[ID:a][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("\\t][INDENT:][ID:b][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("\\t ][INDENT:][ID:c][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("][DEDENT:][-WS:\\t][ID:d][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("][DEDENT:]");
|
||||
_builder_1.newLine();
|
||||
NodeModelTest.assertEquals(_builder_1.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWeirdTree() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("root");
|
||||
_builder.newLine();
|
||||
_builder.append(" ");
|
||||
_builder.append("s4");
|
||||
_builder.newLine();
|
||||
_builder.append(" ");
|
||||
_builder.append("s8");
|
||||
_builder.newLine();
|
||||
_builder.append(" ");
|
||||
_builder.append("s6");
|
||||
_builder.newLine();
|
||||
final ICompositeNode tree = this.getRootNode(_builder);
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[ID:root][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append(" ");
|
||||
_builder_1.append("][INDENT:][ID:s4][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append(" ");
|
||||
_builder_1.append("][INDENT:][ID:s8][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("][DEDENT:][-WS: ][INDENT:][ID:s6][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("][DEDENT:][DEDENT:]");
|
||||
_builder_1.newLine();
|
||||
NodeModelTest.assertEquals(_builder_1.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWeirdTreeEof() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("root");
|
||||
_builder.newLine();
|
||||
_builder.append(" ");
|
||||
_builder.append("s4");
|
||||
_builder.newLine();
|
||||
_builder.append(" ");
|
||||
_builder.append("s8");
|
||||
_builder.newLine();
|
||||
_builder.append(" ");
|
||||
_builder.append("s6");
|
||||
_builder.newLine();
|
||||
final ICompositeNode tree = this.getRootNode(_builder.toString().trim());
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[ID:root][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append(" ");
|
||||
_builder_1.append("][INDENT:][ID:s4][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append(" ");
|
||||
_builder_1.append("][INDENT:][ID:s8][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("][DEDENT:][-WS: ][INDENT:][ID:s6][DEDENT:][DEDENT:]");
|
||||
_builder_1.newLine();
|
||||
NodeModelTest.assertEquals(_builder_1.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_1() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("first");
|
||||
_builder.newLine();
|
||||
_builder.append("\t");
|
||||
_builder.newLine();
|
||||
_builder.append("second");
|
||||
_builder.newLine();
|
||||
final ICompositeNode tree = this.getRootNode(_builder);
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[ID:first][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("\\t");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("][ID:second][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("]");
|
||||
_builder_1.newLine();
|
||||
NodeModelTest.assertEquals(_builder_1.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_2() {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("\"first\"");
|
||||
_builder.newLine();
|
||||
_builder.append("\t");
|
||||
_builder.newLine();
|
||||
_builder.append("\"second\"");
|
||||
_builder.newLine();
|
||||
final ICompositeNode tree = this.getRootNode(_builder);
|
||||
StringConcatenation _builder_1 = new StringConcatenation();
|
||||
_builder_1.append("[STRING:\"first\"][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("\\t");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("][STRING:\"second\"][-WS:");
|
||||
_builder_1.newLine();
|
||||
_builder_1.append("]");
|
||||
_builder_1.newLine();
|
||||
NodeModelTest.assertEquals(_builder_1.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_3() {
|
||||
final ICompositeNode tree = this.getRootNode("first\n\t");
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("[ID:first][-WS:");
|
||||
_builder.newLine();
|
||||
_builder.append("\\t]");
|
||||
_builder.newLine();
|
||||
NodeModelTest.assertEquals(_builder.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_4() {
|
||||
final ICompositeNode tree = this.getRootNode("first\n\t\tabc\n\t");
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("[ID:first][-WS:");
|
||||
_builder.newLine();
|
||||
_builder.append("\\t\\t][INDENT:][ID:abc][-WS:");
|
||||
_builder.newLine();
|
||||
_builder.append("][DEDENT:][-WS:\\t]");
|
||||
_builder.newLine();
|
||||
NodeModelTest.assertEquals(_builder.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_5() {
|
||||
final ICompositeNode tree = this.getRootNode("first\n\t\tabc\n\n");
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("[ID:first][-WS:");
|
||||
_builder.newLine();
|
||||
_builder.append("\\t\\t][INDENT:][ID:abc][-WS:");
|
||||
_builder.newLine();
|
||||
_builder.append("][DEDENT:][-WS:");
|
||||
_builder.newLine();
|
||||
_builder.append("]");
|
||||
_builder.newLine();
|
||||
NodeModelTest.assertEquals(_builder.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_6() {
|
||||
final ICompositeNode tree = this.getRootNode("first\n\t\tabc\n\t\n");
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("[ID:first][-WS:");
|
||||
_builder.newLine();
|
||||
_builder.append("\\t\\t][INDENT:][ID:abc][-WS:");
|
||||
_builder.newLine();
|
||||
_builder.append("][DEDENT:][-WS:\\t");
|
||||
_builder.newLine();
|
||||
_builder.append("]");
|
||||
_builder.newLine();
|
||||
NodeModelTest.assertEquals(_builder.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines_7() {
|
||||
final ICompositeNode tree = this.getRootNode("a\n\tb\n\t ");
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("[ID:a][-WS:");
|
||||
_builder.newLine();
|
||||
_builder.append("\\t][INDENT:][ID:b][-WS:");
|
||||
_builder.newLine();
|
||||
_builder.append("\\t ][DEDENT:]");
|
||||
_builder.newLine();
|
||||
NodeModelTest.assertEquals(_builder.toString().trim(), this.asText(tree));
|
||||
}
|
||||
|
||||
private ICompositeNode getRootNode(final CharSequence seq) {
|
||||
try {
|
||||
final Tree model = this.parseHelper.parse(seq.toString().replaceAll("\r\n", "\n"));
|
||||
final ICompositeNode result = NodeModelUtils.getNode(model).getRootNode();
|
||||
this.invariantChecker.checkInvariant(result);
|
||||
return result;
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
private String asText(final INode node) {
|
||||
final Function1<ILeafNode, Boolean> _function = (ILeafNode it) -> {
|
||||
EObject _grammarElement = it.getGrammarElement();
|
||||
return Boolean.valueOf((!(_grammarElement instanceof Action)));
|
||||
};
|
||||
final Function1<ILeafNode, CharSequence> _function_1 = (ILeafNode it) -> {
|
||||
String _xifexpression = null;
|
||||
boolean _isHidden = it.isHidden();
|
||||
if (_isHidden) {
|
||||
_xifexpression = "-";
|
||||
} else {
|
||||
_xifexpression = "";
|
||||
}
|
||||
String _kenType = this.tokenType(it.getGrammarElement());
|
||||
String _plus = (_xifexpression + _kenType);
|
||||
String _plus_1 = (_plus + ":");
|
||||
String _replace = it.getText().replace("\t", "\\t");
|
||||
return (_plus_1 + _replace);
|
||||
};
|
||||
return IterableExtensions.<ILeafNode>join(IterableExtensions.<ILeafNode>filter(node.getLeafNodes(), _function), "[", "][", "]", _function_1);
|
||||
}
|
||||
|
||||
private String tokenType(final EObject obj) {
|
||||
String _switchResult = null;
|
||||
boolean _matched = false;
|
||||
if (obj instanceof RuleCall) {
|
||||
_matched=true;
|
||||
_switchResult = ((RuleCall)obj).getRule().getName();
|
||||
}
|
||||
if (!_matched) {
|
||||
if (obj instanceof Keyword) {
|
||||
_matched=true;
|
||||
_switchResult = ((Keyword)obj).getValue();
|
||||
}
|
||||
}
|
||||
if (!_matched) {
|
||||
if (obj instanceof AbstractRule) {
|
||||
_matched=true;
|
||||
_switchResult = ((AbstractRule)obj).getName();
|
||||
}
|
||||
}
|
||||
return _switchResult;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue