From 32cfbbe19618096ba74b541694f0b78cf9ded436 Mon Sep 17 00:00:00 2001 From: Christian Dietrich Date: Tue, 26 Apr 2016 16:37:10 +0200 Subject: [PATCH] [492456] Added support for ignorecase-keywords to the new serializer Signed-off-by: Christian Dietrich --- .../antlr/XtextAntlrGeneratorFragment2.xtend | 2 ++ .../tokens/IgnoreCaseKeywordSerializer.java | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 plugins/org.eclipse.xtext/src/org/eclipse/xtext/serializer/tokens/IgnoreCaseKeywordSerializer.java diff --git a/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.xtend b/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.xtend index c519039c6..7f47559e9 100644 --- a/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.xtend +++ b/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.xtend @@ -51,6 +51,7 @@ import org.eclipse.xtext.xtext.generator.util.SyntheticTerminalDetector import static extension org.eclipse.xtext.GrammarUtil.* import static extension org.eclipse.xtext.xtext.generator.model.TypeReference.* import static extension org.eclipse.xtext.xtext.generator.parser.antlr.AntlrGrammarGenUtil.* +import org.eclipse.xtext.serializer.tokens.IKeywordSerializer class XtextAntlrGeneratorFragment2 extends AbstractAntlrGeneratorFragment2 { @@ -458,6 +459,7 @@ class XtextAntlrGeneratorFragment2 extends AbstractAntlrGeneratorFragment2 { if (getOptions().isIgnoreCase()) { rtBindings .addTypeToType(ITokenSerializer.IKeywordSerializer.typeRef, IgnoreCaseKeywordSerializer.typeRef) + .addTypeToType(IKeywordSerializer.typeRef, org.eclipse.xtext.serializer.tokens.IgnoreCaseKeywordSerializer.typeRef) .addTypeToType(AbstractIDValueConverter.typeRef, IgnoreCaseIDValueConverter.typeRef) } rtBindings.contributeTo(language.runtimeGenModule) diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/serializer/tokens/IgnoreCaseKeywordSerializer.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/serializer/tokens/IgnoreCaseKeywordSerializer.java new file mode 100644 index 000000000..6fbc708e9 --- /dev/null +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/serializer/tokens/IgnoreCaseKeywordSerializer.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2010 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.serializer.tokens; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.xtext.Keyword; +import org.eclipse.xtext.nodemodel.INode; +import org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic.Acceptor; + +/** + * @author Christian Dietrich - Initial contribution and API + */ +public class IgnoreCaseKeywordSerializer implements IKeywordSerializer { + + @Override + public boolean isValid(EObject context, Keyword keyword, Object value, Acceptor errorAcceptor) { + if (value instanceof String) { + return keyword.getValue().equalsIgnoreCase((String) value); + } + return false; + } + + @Override + public String serializeAssignedKeyword(EObject context, Keyword keyword, Object value, INode node, Acceptor errorAcceptor) { + if (node != null && node.getGrammarElement() == keyword) + return node.getText(); + return keyword.getValue(); + } + +}