mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 16:28:56 +00:00
Merge pull request #805 from eclipse/cd_issue804
[#804] Added EOF to keyword escape list
This commit is contained in:
commit
b57d7b04ac
3 changed files with 143 additions and 0 deletions
|
@ -0,0 +1,59 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2018 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.xtext.generator
|
||||
|
||||
import org.eclipse.emf.ecore.EPackage
|
||||
import org.eclipse.emf.ecore.xml.type.XMLTypePackage
|
||||
import org.eclipse.xtext.Grammar
|
||||
import org.eclipse.xtext.XtextStandaloneSetup
|
||||
import org.eclipse.xtext.testing.GlobalRegistries
|
||||
import org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento
|
||||
import org.eclipse.xtext.tests.AbstractXtextTests
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.eclipse.xtext.xtext.generator.parser.antlr.KeywordHelper
|
||||
import org.eclipse.xtext.xtext.generator.grammarAccess.GrammarAccessExtensions
|
||||
|
||||
/**
|
||||
* @author Christian Dietrich - Initial contribution and API
|
||||
*/
|
||||
class KeywordHelperTest extends AbstractXtextTests {
|
||||
|
||||
GlobalStateMemento globalStateMemento;
|
||||
|
||||
@Before
|
||||
override setUp() {
|
||||
globalStateMemento = GlobalRegistries.makeCopyOfGlobalState();
|
||||
super.setUp();
|
||||
EPackage.Registry.INSTANCE.put(XMLTypePackage.eNS_URI, XMLTypePackage.eINSTANCE);
|
||||
with(XtextStandaloneSetup)
|
||||
}
|
||||
|
||||
@After
|
||||
override tearDown() {
|
||||
super.tearDown()
|
||||
globalStateMemento.restoreGlobalState();
|
||||
}
|
||||
|
||||
@Test def void testToAntlrTokenIdentifier() {
|
||||
val resource = getResourceFromString('''
|
||||
grammar org.eclipse.xtext.xbase.Xbase with org.eclipse.xtext.common.Terminals
|
||||
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
|
||||
Model returns ecore::EClass : "model" "/EOF" "EOF" "ÄÖÜäöüß" name=ID;
|
||||
''')
|
||||
val grammar = resource.contents.head as Grammar
|
||||
val keywordHelper = new KeywordHelper(grammar, true, new GrammarAccessExtensions)
|
||||
assertEquals("[/EOF, EOF, model, ÄÖÜäöüß]", keywordHelper.allKeywords.sort.toString)
|
||||
assertEquals("KW_EOF_1", keywordHelper.getRuleName("/EOF"))
|
||||
assertEquals("KW_EOF", keywordHelper.getRuleName("EOF"))
|
||||
assertEquals("Model", keywordHelper.getRuleName("model"))
|
||||
assertEquals("AeOeUeaeOeUe", keywordHelper.getRuleName("ÄÖÜäöüß")) //ß is not escaped
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/**
|
||||
* Copyright (c) 2018 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.xtext.xtext.generator;
|
||||
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EPackage;
|
||||
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
|
||||
import org.eclipse.xtend2.lib.StringConcatenation;
|
||||
import org.eclipse.xtext.Grammar;
|
||||
import org.eclipse.xtext.XtextStandaloneSetup;
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.testing.GlobalRegistries;
|
||||
import org.eclipse.xtext.tests.AbstractXtextTests;
|
||||
import org.eclipse.xtext.xbase.lib.Exceptions;
|
||||
import org.eclipse.xtext.xbase.lib.IterableExtensions;
|
||||
import org.eclipse.xtext.xtext.generator.grammarAccess.GrammarAccessExtensions;
|
||||
import org.eclipse.xtext.xtext.generator.parser.antlr.KeywordHelper;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Christian Dietrich - Initial contribution and API
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class KeywordHelperTest extends AbstractXtextTests {
|
||||
private GlobalRegistries.GlobalStateMemento globalStateMemento;
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void setUp() {
|
||||
try {
|
||||
this.globalStateMemento = GlobalRegistries.makeCopyOfGlobalState();
|
||||
super.setUp();
|
||||
EPackage.Registry.INSTANCE.put(XMLTypePackage.eNS_URI, XMLTypePackage.eINSTANCE);
|
||||
this.with(XtextStandaloneSetup.class);
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
@Override
|
||||
public void tearDown() {
|
||||
try {
|
||||
super.tearDown();
|
||||
this.globalStateMemento.restoreGlobalState();
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToAntlrTokenIdentifier() {
|
||||
try {
|
||||
StringConcatenation _builder = new StringConcatenation();
|
||||
_builder.append("grammar org.eclipse.xtext.xbase.Xbase with org.eclipse.xtext.common.Terminals");
|
||||
_builder.newLine();
|
||||
_builder.append("import \"http://www.eclipse.org/emf/2002/Ecore\" as ecore");
|
||||
_builder.newLine();
|
||||
_builder.append("Model returns ecore::EClass : \"model\" \"/EOF\" \"EOF\" \"ÄÖÜäöüß\" name=ID;");
|
||||
_builder.newLine();
|
||||
final XtextResource resource = this.getResourceFromString(_builder.toString());
|
||||
EObject _head = IterableExtensions.<EObject>head(resource.getContents());
|
||||
final Grammar grammar = ((Grammar) _head);
|
||||
GrammarAccessExtensions _grammarAccessExtensions = new GrammarAccessExtensions();
|
||||
final KeywordHelper keywordHelper = new KeywordHelper(grammar, true, _grammarAccessExtensions);
|
||||
Assert.assertEquals("[/EOF, EOF, model, ÄÖÜäöüß]", IterableExtensions.<String>sort(keywordHelper.getAllKeywords()).toString());
|
||||
Assert.assertEquals("KW_EOF_1", keywordHelper.getRuleName("/EOF"));
|
||||
Assert.assertEquals("KW_EOF", keywordHelper.getRuleName("EOF"));
|
||||
Assert.assertEquals("Model", keywordHelper.getRuleName("model"));
|
||||
Assert.assertEquals("AeOeUeaeOeUe", keywordHelper.getRuleName("ÄÖÜäöüß"));
|
||||
} catch (Throwable _e) {
|
||||
throw Exceptions.sneakyThrow(_e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -143,6 +143,7 @@ public class KeywordHelper implements Adapter {
|
|||
result.charAt(0) == '_' // rule names may not start with an underscore
|
||||
|| "System".equals(result) // the generated code contains System.err.printlns which is ambiguous with the generated field name
|
||||
|| result.startsWith("DFA") // the generated code may have fields named DFA... - avoid (unlikely) conflicts
|
||||
|| result.startsWith("EOF") // the generated code may have fields named EOF... - avoid (unlikely) conflicts
|
||||
|| result.startsWith("FOLLOW") // same with FOLLOW_ field names
|
||||
|| result.startsWith("Internal") && result.endsWith("Parser") // same with the name of the class itself
|
||||
) {
|
||||
|
|
Loading…
Reference in a new issue