xText as standalone lexer, parser, interpreter? [message #1695802] |
Tue, 19 May 2015 12:08 |
Dominik Reinert Messages: 14 Registered: May 2015 |
Junior Member |
|
|
Hello,
I want xText to simply check if inputs given by the user are in the correct syntax. If so, it should make a database entry with the specification given by the user.
E.g:
1) User enters:
If [300] ( sensor1.temperature >= 200) then (sendEmail ( 'www.example @given.com'))
(means: if the temperature of sensor 1 is higher than 200 ° for 300 milliseconds, the system should send an email)
2) Is the Syntax correct?
3) xText (?) should interprete (?) this expression as follows:
3.1) Does sensor1 exist? Looking for it in Database Table 'Sensors.ID' (primary key).
3.2) Has sensor1 got 'temperature'? Looking for it in another table as one sensor can have many values for different issues.
3.3) Is action 'sendEmail' valid? Table 'Actions'
3.4) In this particular case: Is the email-adress valid?
4) If all these tests went good, the next step would be creating an entry in the 'rules' table (sensor.ID [foreign key] , temperature, condition, action, parameters for action)
Now my question is, if it is possible to write a standalone client which is able to handle the User input and proceding it with the DSL I wrote in xText. This client should more or less only be like an editor or something like that.
The only things I found where many tutorials on how to use the language generator, but that does not seem to be the kind of thing I need.
Thank you in advance
---------------------------------
The DSL-grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate ruleLanguage "http://www.xtext.org/example/mydsl/MyDsl"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
ruleLanguage:
'if ['timeInMs=INT'] ('conds=CONDITIONS') then ('actions=ACTIONS')';
/* There can be as many conditions as the user wants to */
/* link to another */
CONDITIONS returns Expression:
SCOND ({CONDITIONS.left=current} '&' right=SCOND)*;
/* This is how one condition is set up:*/
/* name << value*/
/* for example: */
/* sensor1.temperature << 100°C*/
SCOND returns Expression:
sname=ID'.'valueLeft=INT COMP valueRight=INT;
/* These are the possible operators (comparison only) */
COMP:
'<<' | '>>' | '==' | '<=' | '>=';
/* The conditions defined earlier can also have as many */
/* actions as the user wants */
ACTIONS returns Expression:
ACTION ({ACTIONS.left=current}'&' right=ACTION)*;
/* An action has a name and gets a parameter list, e.g.:*/
/* sendEmail ("example@googlemail.com") */
ACTION returns Expression:
fname=ID '(' params=PARLIST ')';
/* Whatever comes here should just be given to the */
/* called function of the main program */
PARLIST returns Expression:
MOREPAR ({PARLIST.left=current} ',' right=MOREPAR)*;
MOREPAR returns Expression:
{MOREPAR}MOREPARValue=MOREPARValue ;
MOREPARValue returns ecore::EString:(INT | ID);
[Updated on: Tue, 19 May 2015 12:09] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: xText as standalone lexer, parser, interpreter? [message #1695977 is a reply to message #1695973] |
Wed, 20 May 2015 17:38 |
|
p.s: you are the better swing programmer than me but you can take the following as starting point
package org.xtext.example.mydsl;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.xtext.resource.FileExtensionProvider;
import org.eclipse.xtext.util.StringInputStream;
import org.xtext.example.mydsl.myDsl.Greeting;
import org.xtext.example.mydsl.myDsl.Model;
import com.google.inject.Injector;
public class Runner {
private static JTextArea text;
private static Injector injector;
private static JFrame frame;
public static void main(String[] args) {
injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
frame = new JFrame();
frame.setBounds(new Rectangle(800,600));
frame.setTitle("Demo");
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
text = new JTextArea(80,20);
text.setText("lala");
frame.add(text);
JButton button = new JButton("go");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String t = text.getText();
ResourceSet rs = injector.getInstance(ResourceSet.class);
Resource r = rs.createResource(URI.createURI("dummy." + injector.getInstance(FileExtensionProvider.class).getPrimaryFileExtension()));
try {
r.load(new StringInputStream(t), null);
if (r.getErrors().isEmpty()) {
//validation
EObject myModel = r.getContents().get(0);
Diagnostic diagnostic = Diagnostician.INSTANCE.validate(myModel);
if (Diagnostic.ERROR == diagnostic.getSeverity()) {
//TODO better error handling
for (Diagnostic d : diagnostic.getChildren()) {
JOptionPane.showMessageDialog(frame, "Found Error: " + d.getMessage());
}
return;
}
//TODO warning handling
//work with model
if (myModel instanceof Model) {
for (Greeting g : ((Model) myModel).getGreetings()) {
JOptionPane.showMessageDialog(frame, "Found: " + g.getName());
}
}
} else {
//TODO better error handling
for (org.eclipse.emf.ecore.resource.Resource.Diagnostic error : r.getErrors()) {
JOptionPane.showMessageDialog(frame, "Found Error: " + error.getMessage());
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
frame.add(button);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Powered by
FUDForum. Page generated in 0.04165 seconds