What is the right way of doing syntax coloring in a grammar? I believe it was like this:
syntax MappingName = #category="Constant" mappingname: Id mapping;
But it doesn't work for me. The file is just black text without parse errors (and it is open in the IMP editor, and I saw it create a parser).
For inspiration, you can always look at the Rascal grammar. Where you can see that your syntax and category name seem correct.
Try what happens if you have no name for the production?
syntax MappingName = #category="Constant" Id mapping;
Related
When using ANTLR to parse XML, can ANTLR validate that an end tag matches its start tag? The XML parser in the ANTLR book doesn’t check for this.
I could imagine a generic approach like this (but never actually tried it myself):
tag: openTag = TAG_OPEN content closeTag = TAG_CLOSE { tagsMatch($openTag, $closeTag); }?;
You'd use a validating predicate to fail the entire rule if the tag parts don't match. Might give you problems with error reporting, but that can be solved.
For arbitrary XML tags, a context free parser can't do this. ANTLR in its pure state is essentially context-free.
You can hack most parsers (probably including ANTLR) to build a tag stack. When <tagname... gets parsed (or lexed as you find convenient), you can push the tag name on the stack. When </tagname... is parsed/lexed, you can match the tagname to the top of the stack, and complain if a mismatch occurs.
I used the lexer version in my XML parser (see bio), seems to work pretty well.
I am trying to make a regex that will find all the commented code in Xcode project. Note that result of Regex must only contain commented code and must not contain the comments that we add to make the project more understandable.
Result of regex searching not contain the line like below
// this comment is added to make the function understandable
But contain the lines like
// [super viewWillDisappear:animated];
I successfully make my required expression and it is
^.*/[/,\*].*;.*$
This will only find your single line or multiline commented code.
To anyone wondering how to use regular expression
search "^./[/,*].;.*$" after selecting regular expression option from find menu after doing global search(command + shift + F)
I'm using NHapi v22 tool for sending HL7 messages. Issue is I'm not able to create component separator(^) and new line. Please tell me how to code for it in C#.
Following is the code:
MSH|^~\&|xyz|xyz|FLOW|FLOW|201601201525||ADT\S\A04|201601201525123456789|P|2.3|||NE|NE
I need to get like this- ADT^A04
I believe the issue here is that \S\ is an escape character for ^ (component separator). Your message should contain an actual component separator with the actual ^ in it, not the escape character.
"ADT" should appear in MSH-9-1 and "A04" should appear in MSH-9-2 as opposed to how I suspect you are doing it with "ADT^A01" in MSH-9.
Does this help? Please feel free to contact me directly if you need more specialized advice.
edit: I don't normally use NHapi but I suspect you may need to do something along this line:
terser.set("/MSH-9-1", "ADT");
terser.set("/MSH-9-2", "A01");
as opposed to something you might be doing
terser.set("/MSH-9", "ADT^A01");
I need to parse something during the runtime of my eclipse plugin (created with Xtext) which would then be treated as if the user has typed it in but without actually popping up and beeing visible for the user as an input of himself.
For exmample I have this input:
for "i" from 1 to 3 do {};
My problem woth this input is that the variable i is not declared as a normal declaration with a "=" but I need the parser to tell that it is one. So I want to let the parser parse
i = 1;
so it recognizes it as a normal declaration and allows cross-references to it.
Greeting Krzmbrzl
EDIT:
All in all the thing I want is to add a statement i=1; to the AST
I just want to have eclipse support for an existing language so I'm not writing an interpreter nor a generator. The problem is that when I have a for-loop like above the actual interpreter of that language declares a variable i (or however it's named in the loop header) and therefore this variable is available in the loop body. In my case my parser doesn't recognise i as a variable because it only knows that a declaration is done via "=" so I can't use i in the loop body (if I try so I get the error that the declaration i cannot be resolved). This is why I want to add this declaration manually when such a loop is created. I don't need to worry about any compiling or interpreting difficulties because I don't do this myself. As I already said I just want to have all the cool eclipse features for this language.
Ok, now I understand your problem. It is still no good idea to add any element to the AST to resolve any cross reference! You don't do that! Instead you should try to refactor your grammar in a way that "i" in for "i" from ... is a compatible declaration of a variable. There are several tricks to do that. Have you completely read the Xtext documentation? Have also read the Xtext book? Both documents tell a lot about how to make Xtext do things you will not expect.
Anyway, two tricks I often use are:
Introduce an unused, abstract Parser Rule which you can then use as destination of a cross reference, but which is never used as an attribute (containment reference).
AbstractDecl:
VarDecl | ForVarDecl;
VarDecl:
name=ID ...;
ForVarDecl:
'"' name=ID '"';
For:
'for' decl=ForVarDecl 'from' from=INT 'to' to=INT 'do' block=Block;
...
StatementWithCR:
ref=[AbstractDecl] ...;
Define any ParserRule, which returns an other type.
ForDecl returns VarDecl:
'"' name=ID '"';
If you would post the grammar which corresponds to this specific problem, we could develop a solution which is safe. Adding anything to the AST during live processing content of the editor will lead to a faulty state which can destroy your document.
I need to Escape a reserved word to use it as an identifier in Delphi. I thought that was accomplished by using the ampersand "&", but that doesn't seem to be working right. Any other suggestions?
I found the doc page (search for Extended Syntax) on it, and it should be ampersand.
Figured out the problem. The ampersand works for compiling and error insight, but not code completion. Good to know. I had to add an _ suffix to get code completion to work, then change it back afterwords. I should check QC for a bug report.