-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixed * Cleaned * Fixed * Fixed implicit item bug * Fixed escaping
- Loading branch information
1 parent
61001fa
commit 7a160bc
Showing
3 changed files
with
115 additions
and
3 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
51 changes: 51 additions & 0 deletions
51
rosetta-lang/src/main/java/com/regnosys/rosetta/parsing/ValidIDConverter.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,51 @@ | ||
package com.regnosys.rosetta.parsing; | ||
|
||
import java.util.Set; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.eclipse.xtext.Alternatives; | ||
import org.eclipse.xtext.Keyword; | ||
import org.eclipse.xtext.ParserRule; | ||
import org.eclipse.xtext.conversion.ValueConverterException; | ||
import org.eclipse.xtext.conversion.impl.AbstractValueConverter; | ||
import org.eclipse.xtext.conversion.impl.IDValueConverter; | ||
import org.eclipse.xtext.nodemodel.INode; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.regnosys.rosetta.services.RosettaGrammarAccess; | ||
|
||
/** | ||
* Allows using a caret `^` to escape identifiers based on the `ValidID` rule. | ||
* | ||
* Similar implementation to {@link org.eclipse.xtext.conversion.impl.IDValueConverter}. | ||
*/ | ||
public class ValidIDConverter extends AbstractValueConverter<String> { | ||
private final ParserRule validIDRule; | ||
private final Set<String> validKeywordIDs; | ||
private final IDValueConverter delegate; | ||
@Inject | ||
public ValidIDConverter(RosettaGrammarAccess grammarAccess, IDValueConverter delegate) { | ||
this.validIDRule = grammarAccess.getValidIDRule(); | ||
this.delegate = delegate; | ||
|
||
// We expect the ValidID rule to be of the form `ID | <list of keywords>` | ||
Alternatives alternatives = (Alternatives) validIDRule.getAlternatives(); | ||
ImmutableSet.Builder<String> builder = ImmutableSet.builder(); | ||
alternatives.getElements().stream().skip(1).forEach(element -> builder.add(((Keyword)element).getValue())); | ||
validKeywordIDs = builder.build(); | ||
} | ||
|
||
@Override | ||
public String toValue(String string, INode node) { | ||
return delegate.toValue(string, node); | ||
} | ||
|
||
@Override | ||
public String toString(String value) throws ValueConverterException { | ||
if (validKeywordIDs.contains(value)) { | ||
return value; | ||
} | ||
return delegate.toString(value); | ||
} | ||
} |
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