Home » Modeling » TMF (Xtext) » Checking if a ID matches some string
Checking if a ID matches some string [message #1701262] |
Fri, 10 July 2015 10:54 |
Anton Bergman Messages: 34 Registered: June 2015 |
Member |
|
|
I have the standard ID terminal from common terminals. I now have a long list of Colors:
MediumTurquoise
MediumVioletRed
MidnightBlue
MintCream
MistyRose
Moccasin
NavajoWhite
Navy
OldLace
Olive
OliveDrab
Orange
OrangeRed
Orchid
PaleGoldenRod
PaleGreen
PaleTurquoise
PaleVioletRed
PapayaWhip
PeachPuff
Peru
Pink
etc. Is there any way of checking if the ID exists in this list, instead of creating a separate rule for colors that I have to match against?
Some other way than this:
Actor:
'actor' name=ID ('#' Color)?
Color:
('AliceBlue'|'AntiqueWhite'|'Aqua'|'Aquamarine'|'Azure'|'Beige'|'Bisque'|'Black'|'BlanchedAlmond'
|'Blue'|'BlueViolet'|'Brown'|'BurlyWood'|'CadetBlue'|'Chartreuse'|'Chocolate'|'Coral'
|'CornflowerBlue'|'Cornsilk'|'Crimson'|'Cyan'|'DarkBlue'|'DarkCyan'|'DarkGoldenRod'|'DarkGray'
|'DarkGreen'|'DarkGrey'|'DarkKhaki'|'DarkMagenta'|'DarkOliveGreen'|'DarkOrchid'|'DarkRed'
|'DarkSalmo'|'DarkSeaGreen'|'DarkSlateBlue'|'DarkSlateGray'|'DarkSlateGrey'|'DarkTurquoise'
|'DarkViolet'|'Darkorange'|'DeepPink'|'DeepSkyBlue'|'DimGray'|'DimGrey'|'DodgerBlue'
|'FireBrick'|'FloralWhite'|'ForestGreen'|'Fuchsia'|'Gainsboro'|'GhostWhite'|'Gold'
|'GoldenRod'|'Gray'|'Green'|'GreenYellow'|'Grey'|'HoneyDew'|'HotPink'|'IndianRed'
|'Indigo'|'Ivory'|'Khaki'|'Lavender'|'LavenderBlush'|'LawnGreen'|'LemonChiffon'
|'LightBlue'|'LightCoral'|'LightCyan'|'LightGoldenRodYellow'|'LightGray'|'LightGreen'
|'LightGrey'|'LightPink'|'LightSalmon'|'LightSeaGreen'|'LightSkyBlue'|'LightSlateGray'
|'LightSlateGrey'|'LightSteelBlue'|'LightYellow'|'Lime'|'LimeGreen'|'Linen'|'Magenta'
|'Maroon'|'MediumAquaMarine'|'MediumBlue'|'MediumOrchid'|'MediumPurple'|'MediumSeaGreen'
|'MediumSlateBlue'|'MediumSpringGreen'|'MediumTurquoise'|'MediumVioletRed'
|'MidnightBlue'|'MintCream'|'MistyRose'|'Moccasin'|'NavajoWhite'|'Navy'|'OldLace'
|'Olive'|'OliveDrab'|'OrangeRed'|'Orchid'|'PaleGoldenRod'|'PaleGreen'
|'PaleTurquoise'|'PaleVioletRed'|'PapayaWhip'|'PeachPuff'|'Peru'|'Pink'|'Plum'
|'PowderBlue'|'Purple'|'Red'|'RosyBrown'|'RoyalBlue'|'SaddleBrown'
|'Salmon'|'SandyBrown'|'SeaGreen'|'Sienna'|'Silver'|'SkyBlue'|'SlateBlue'
|'SlateGray'|'SlateGrey'|'Snow'|'SpringGreen'|'SteelBlue'|'Tan'|'Teal'|'Thistle'
|'Tomato'|'Turquoise'|'Violet'|'Wheat'|'White'|'WhiteSmoke'|'Yellow'|'YellowGreen')
;
[Updated on: Fri, 10 July 2015 10:57] Report message to a moderator
|
|
| | | | | | | |
Re: Checking if a ID matches some string [message #1701375 is a reply to message #1701370] |
Mon, 13 July 2015 07:07 |
|
Sure you did the grammar right? and your implementation of the valueconverter looks strange since it disallows colors. and you should not compare using ==
@Override
public String toValue(String string, INode node) throws ValueConverterException{
if (!org.eclipse.xtext.util.Arrays.contains(colorArray, string)) {
throw new ValueConverterException("Not a Color", node, null);
}
return string;
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
| | | | |
Re: Checking if a ID matches some string [message #1701392 is a reply to message #1701391] |
Mon, 13 July 2015 08:17 |
|
The following works for me
Model:
greetings+=Greeting*;
Greeting:
'Hello' name=COLOR '!';
COLOR: ID; public class PlantumlValueConverter extends DefaultTerminalConverters {
boolean Exists = false;
String[] colorArray = {
"AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black"
,"BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse"
,"Chocolate","Coral","CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan"
,"DarkGoldenRod","DarkGray","DarkGreen","DarkGrey","DarkKhaki","DarkMagenta","DarkOliveGreen"
,"DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGray"
,"DarkSlateGrey","DarkTurquoise","DarkViolet","Darkorange","DeepPink","DeepSkyBlue"
,"DimGray","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia"
,"Gainsboro","GhostWhite","Gold","GoldenRod","Gray","Green","GreenYellow","Grey"
,"HoneyDew","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender","LavenderBlush"
,"LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGoldenRodYellow"
,"LightGray","LightGreen","LightGrey","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue"
,"LightSlateGray","LightSlateGrey","LightSteelBlue","LightYellow","Lime","LimeGreen","Linen"
,"Magenta","Maroon","MediumAquaMarine","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen"
,"MediumSlateBlue","MediumSpringGreen","MediumTurquoise","MediumVioletRed","MidnightBlue"
,"MintCream","MistyRose","Moccasin","NavajoWhite","Navy","OldLace","Olive","OliveDrab"
,"Orange","OrangeRed","Orchid","PaleGoldenRod","PaleGreen","PaleTurquoise","PaleVioletRed"
,"PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue","Psurple","Red","RosyBrown"
,"RoyalBlue","SaddleBrown","Salmon","SandyBrown","SeaGreen","SeaShell"
,"Sienna","Silver","SkyBlue","SlateBlue","SlateGray","SlateGrey","Snow","SpringGreen"
,"SteelBlue","Tan","Teal","Thistle","Tomato","Turquoise","Violet","Wheat","White"
,"WhiteSmoke","Yellow","YellowGreen"
};
@ValueConverter(rule = "COLOR")
public IValueConverter<String> COLOR(){
return new IValueConverter<String>(){
@Override
public String toValue(String string, INode node) throws ValueConverterException{
if (!org.eclipse.xtext.util.Arrays.contains(colorArray, string)) {
throw new ValueConverterException("Not a Color", node, null);
}
return string;
}
@Override
public String toString(String value)
throws ValueConverterException {
// TODO Auto-generated method stub
return value;
}
};
}
} @Override
public Class<? extends IValueConverterService> bindIValueConverterService() {
return PlantumlValueConverter.class;
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
| | | | | | | | | |
Re: Checking if a ID matches some string [message #1701453 is a reply to message #1701435] |
Mon, 13 July 2015 14:02 |
Anton Bergman Messages: 34 Registered: June 2015 |
Member |
|
|
I try it simple, with this grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
greetings+=Greeting*;
Greeting:
'Hello' name=ID Color '!';
Color: ID;
This valueconverter:
package org.xtext.example.mydsl;
import org.eclipse.xtext.common.services.DefaultTerminalConverters;
import org.eclipse.xtext.conversion.IValueConverter;
import org.eclipse.xtext.conversion.ValueConverter;
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.util.Strings;
public class PlantumlValueConverter extends DefaultTerminalConverters {
boolean Exists = false;
String[] colorArray = {
"AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black"
,"BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse"
,"Chocolate","Coral","CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan"
,"DarkGoldenRod","DarkGray","DarkGreen","DarkGrey","DarkKhaki","DarkMagenta","DarkOliveGreen"
,"DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGray"
,"DarkSlateGrey","DarkTurquoise","DarkViolet","Darkorange","DeepPink","DeepSkyBlue"
,"DimGray","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia"
,"Gainsboro","GhostWhite","Gold","GoldenRod","Gray","Green","GreenYellow","Grey"
,"HoneyDew","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender","LavenderBlush"
,"LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGoldenRodYellow"
,"LightGray","LightGreen","LightGrey","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue"
,"LightSlateGray","LightSlateGrey","LightSteelBlue","LightYellow","Lime","LimeGreen","Linen"
,"Magenta","Maroon","MediumAquaMarine","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen"
,"MediumSlateBlue","MediumSpringGreen","MediumTurquoise","MediumVioletRed","MidnightBlue"
,"MintCream","MistyRose","Moccasin","NavajoWhite","Navy","OldLace","Olive","OliveDrab"
,"Orange","OrangeRed","Orchid","PaleGoldenRod","PaleGreen","PaleTurquoise","PaleVioletRed"
,"PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue","Psurple","Red","RosyBrown"
,"RoyalBlue","SaddleBrown","Salmon","SandyBrown","SeaGreen","SeaShell"
,"Sienna","Silver","SkyBlue","SlateBlue","SlateGray","SlateGrey","Snow","SpringGreen"
,"SteelBlue","Tan","Teal","Thistle","Tomato","Turquoise","Violet","Wheat","White"
,"WhiteSmoke","Yellow","YellowGreen"
};
@ValueConverter(rule = "Color")
public IValueConverter<String> Color(){
return new IValueConverter<String>(){
@Override
public String toValue(String string, INode node) throws ValueConverterException{
System.out.println("Test print !! ");
if (!org.eclipse.xtext.util.Arrays.contains(colorArray, string)) {
throw new ValueConverterException("Not a Color", node, null);
}
return string;
}
@Override
public String toString(String value)
throws ValueConverterException {
// TODO Auto-generated method stub
return value;
}
};
}
}
And this runtimemodule:
/*
* generated by Xtext
*/
package org.xtext.example.mydsl;
import org.eclipse.xtext.conversion.IValueConverterService;
/**
* Use this class to register components to be used at runtime / without the Equinox extension registry.
*/
public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {
@Override
public Class<? extends IValueConverterService> bindIValueConverterService() {
return PlantumlValueConverter.class;
}
}
And no errors are thrown, and no sysout is written, when I for example test the DSL with
[Updated on: Mon, 13 July 2015 14:03] Report message to a moderator
|
|
| | |
Re: Checking if a ID matches some string [message #1701471 is a reply to message #1701392] |
Mon, 13 July 2015 14:46 |
|
and please try this test
import javax.inject.Inject
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.junit4.util.ParseHelper
import org.junit.Test
import org.junit.runner.RunWith
import org.xtext.example.mydsl2.MyDslInjectorProvider
import org.xtext.example.mydsl2.myDsl.Model
import org.eclipse.xtext.junit4.validation.ValidationTestHelper
@InjectWith(MyDslInjectorProvider)
@RunWith(XtextRunner)
class MyDslParserTest {
@Inject extension ParseHelper<Model>
@Inject extension ValidationTestHelper
@Test
def void testIt() {
'''Hello NoColor!'''.parse.assertNoErrors
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: Checking if a ID matches some string [message #1701556 is a reply to message #1701471] |
Tue, 14 July 2015 07:03 |
Anton Bergman Messages: 34 Registered: June 2015 |
Member |
|
|
package org.xtext.example.mydsl.tests
import javax.inject.Inject
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.junit4.util.ParseHelper
import org.junit.Test
import org.junit.runner.RunWith
import org.eclipse.xtext.junit4.validation.ValidationTestHelper
import org.xtext.example.mydsl.MyDslInjectorProvider
import org.xtext.example.mydsl.myDsl.Model
@InjectWith(MyDslInjectorProvider)
@RunWith(XtextRunner)
class MyDslParserTest {
@Inject extension ParseHelper<Model>
@Inject extension ValidationTestHelper
@Test
def void testIt() {
'''Hello NoColor !'''.parse.assertNoErrors
}
}
Tests without any errors with the grammar
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
greetings+=Greeting*;
Greeting:
'Hello' Color '!';
Color:
ID
;
All i get in the console is this, but it guess most of it is unrelated:
!SESSION 2015-07-14 08:59:56.082 -----------------------------------------------
eclipse.buildId=4.4.2.M20150204-1700
java.version=1.8.0_45
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Framework arguments: -version 3 -port 64020 -testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader -loaderpluginname org.eclipse.jdt.junit4.runtime -classNames org.xtext.example.mydsl.tests.MyDslParserTest -application org.eclipse.pde.junit.runtime.uitestapplication -product org.eclipse.platform.ide -testpluginname org.xtext.example.mydsl.tests
Command-line arguments: -os win32 -ws win32 -arch x86_64 -consoleLog -version 3 -port 64020 -testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader -loaderpluginname org.eclipse.jdt.junit4.runtime -classNames org.xtext.example.mydsl.tests.MyDslParserTest -application org.eclipse.pde.junit.runtime.uitestapplication -product org.eclipse.platform.ide -data C:\Users\eaonban\Downloads\eclipse-clean\workspace-clean/../junit-workspace -dev file:C:/Users/eaonban/Downloads/eclipse-clean/workspace-clean/.metadata/.plugins/org.eclipse.pde.core/pde-junit/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog -testpluginname org.xtext.example.mydsl.tests
!ENTRY org.eclipse.egit.ui 2 0 2015-07-14 09:00:27.445
!MESSAGE Warning: EGit couldn't detect the installation path "gitPrefix" of native Git. Hence EGit can't respect system level
Git settings which might be configured in ${gitPrefix}/etc/gitconfig under the native Git installation directory.
The most important of these settings is core.autocrlf. Git for Windows by default sets this parameter to true in
this system level configuration. The Git installation location can be configured on the
Team > Git > Configuration preference page's 'System Settings' tab.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.egit.ui 2 0 2015-07-14 09:00:27.449
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'H:\'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.core.net 4 0 2015-07-14 09:00:30.300
!MESSAGE WinHttp.DetectAutoProxyConfigUrl for wpad failed with error 'The Proxy Auto-configuration URL was not found.
' #12180.
[Updated on: Tue, 14 July 2015 07:04] Report message to a moderator
|
|
| | | | | |
Goto Forum:
Current Time: Tue Mar 19 06:07:15 GMT 2024
Powered by FUDForum. Page generated in 0.08169 seconds
|