-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from kris7t/quoted-names
Quoted names
- Loading branch information
Showing
30 changed files
with
744 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
subprojects/language/src/main/java/tools/refinery/language/conversion/IDValueConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/> | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package tools.refinery.language.conversion; | ||
|
||
import org.eclipse.xtext.Grammar; | ||
import org.eclipse.xtext.conversion.impl.AbstractIDValueConverter; | ||
|
||
import java.util.Set; | ||
|
||
public class IDValueConverter extends AbstractIDValueConverter { | ||
@Override | ||
protected Set<String> computeValuesToEscape(Grammar grammar) { | ||
return Set.of(); | ||
} | ||
|
||
@Override | ||
protected boolean mustEscape(String value) { | ||
// Do not escape keywords with ^, because we use single quotes instead in {@link IdentifierValueConverter}. | ||
return false; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...s/language/src/main/java/tools/refinery/language/conversion/IdentifierValueConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/> | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package tools.refinery.language.conversion; | ||
|
||
import com.google.inject.Inject; | ||
import org.eclipse.xtext.GrammarUtil; | ||
import org.eclipse.xtext.conversion.IValueConverter; | ||
import org.eclipse.xtext.conversion.ValueConverterException; | ||
import org.eclipse.xtext.nodemodel.INode; | ||
import tools.refinery.language.naming.NamingUtil; | ||
import tools.refinery.language.parser.antlr.IdentifierTokenProvider; | ||
import tools.refinery.language.services.ProblemGrammarAccess; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
public class IdentifierValueConverter implements IValueConverter<String> { | ||
private final Set<String> keywords; | ||
private final QUOTED_IDValueConverter quotedIdValueConverter; | ||
|
||
@Inject | ||
public IdentifierValueConverter( | ||
ProblemGrammarAccess grammarAccess, QUOTED_IDValueConverter quotedIdValueConverter, | ||
IdentifierTokenProvider identifierTokenProvider) { | ||
this.quotedIdValueConverter = quotedIdValueConverter; | ||
quotedIdValueConverter.setRule(grammarAccess.getQUOTED_IDRule()); | ||
keywords = new LinkedHashSet<>(GrammarUtil.getAllKeywords(grammarAccess.getGrammar())); | ||
keywords.removeAll(identifierTokenProvider.getIdentifierKeywords()); | ||
} | ||
|
||
@Override | ||
public String toValue(String string, INode node) throws ValueConverterException { | ||
if (string == null) { | ||
return null; | ||
} | ||
if (NamingUtil.isQuoted(string)) { | ||
return quotedIdValueConverter.toValue(string, node); | ||
} | ||
return string; | ||
} | ||
|
||
@Override | ||
public String toString(String value) throws ValueConverterException { | ||
if (value == null) { | ||
throw new ValueConverterException("Identifier may not be null.", null, null); | ||
} | ||
if (NamingUtil.isSimpleId(value) && !keywords.contains(value)) { | ||
return value; | ||
} | ||
return quotedIdValueConverter.toString(value); | ||
} | ||
} |
Oops, something went wrong.