Does a grammar exist for the language?

+4 votes
asked Jan 8, 2013 in Won't implement by anonymous
For to develop integration into IntelliJ it would be very helpful to have a grammar. Best would be a BNF grammar. Does a grammar exist for the PlantUML language? I know there is the language reference document. But to infer the grammar out of this document seems to me very extensive.

1 Answer

+1 vote
answered Jan 15, 2013 by plantuml (295,000 points)
No, there is no BNF grammar. Parsing is done by regular expression.

You will find some information here about checking syntax of a text file.

The -syntax flag may be usefull for developping integration with IntelliJ, and if you wish, we could also provide an API to allow syntax checking from Java code.
commented Jan 15, 2013 by stefan (100 points)
Thanks for the answer.
It seems to be a good approach. I can use the list of language elements for highlighting types, keywords etc. And I could delegate the syntax checking to your proposed API. I'm going to think about requirements for that API, since there are different possibilities for the source structure to test: ascii source code, AST, ... (?)
Well, I'll come back to this.

Cheers, Stefan
commented Jan 17, 2013 by plantuml (295,000 points)
To be able to make some tests, a first alpha solution has been implemented in version 7950.
This is not the final solution, just a way to discuss about the API :

The net.sourceforge.plantuml.syntax.SyntaxChecker provides some static methods checkSyntax() that takes a List<String>
containing the source, and returns a net.sourceforge.plantuml.syntax.SyntaxResult object that gives information about the syntax.

See examples:

        final List<String> s = new ArrayList<String>();

        final SyntaxResult syntaxResult = SyntaxChecker.checkSyntax(s);
        assertEquals(UmlDiagramType.SEQUENCE, syntaxResult.getUmlDiagramType());

Another example :

        final List<String> s = new ArrayList<String>();
        s.add("actor bob");
        s.add("actro bob"); // Typo on this line

        final SyntaxResult syntaxResult = SyntaxChecker.checkSyntax(s);
        assertTrue(syntaxResult.isError()); // True because there is a syntax error
        assertEquals(2, syntaxResult.getErrorLinePosition()); // Line where the error happens
        assertEquals("[Syntax Error?]", syntaxResult.getErrors().toString());
        assertEquals("[Did you mean:, actor bob, actrobob]", syntaxResult.getSuggest().toString());
commented Jan 23, 2013 by stefan (100 points)
edited Jan 25, 2013 by stefan
I did some tests and I was able to create a first version of a syntax checker for intellij idea. It marks the line of error and provide quick fix of errors based on the suggestions of the SyntaxResult. I've got some questions
1. I have to ship the file plantuml.jar in the plugin. Since plantuml is under GPL licence does the plugin also has to be under GPL licence?
2. Could you extend the API to check required @startuml and @enduml?
3. Currently there is a IndexOutOfBoundException if @startuml is present but not @enduml. An error on last line would be appreciated with suggestion add @enduml

PS: Here's the plugin:
commented Jan 27, 2013 by plantuml (295,000 points)
1. Yes, sorry about that. Note that you can also use the LGPL version of PlantUML if this is an issue for you, and use LGPL for your plugin.
2 and 3 : this should be corrected in Version 7953. Thanks for posting if you find other issues.

And tell us when your plugin will be mature, we will post a news about it.
Thanks for your work.
commented Jan 28, 2013 by stefan (100 points)
Hi, I upgraded to version 7953. One question and one issue regarding to SyntaxChecher:
1. Tag @startuml must appear on first line. What about to let startuml somewhere else in the document and having in the lines before @startuml comments?
2. Tag @enduml must be followed by a newline character. My feature request is to also allow @endline followed by EOF :-)

commented Jan 29, 2013 by plantuml (295,000 points)
Question 2 should be ok on release 7954.

About question 1, I think that the API should be simple : On single String (or List<String>) that contains only one diagram, and the String/List<String> should only contain the PlantUML syntax description.
Removing characters before @startuml should be done by the caller of the API, and not the API itself (well, that's my opinion :-)

I hope that it's not an issue for you.
commented Jun 30, 2014 by dzmitry.lahoda (140 points)
Looks like this whole project can be good object for rewrite in language workbench like jetbrains mps or eclipse xtext.