Search code examples
markdownxtext

XText grammar for markdown


I'm writing XText grammar for Markdown syntax. In markdown the h1 can be written with #heading.

So, Heading can match anything other than a newline character.

grammar org.example.domainmodel.DomainModel with org.eclipse.xtext.common.Terminals

generate domainModel "http://www.example.org/domainmodel/DomainModel"

DomainModel:
    (elements += Element)*
;

Element:
    Section1 | Section2
;

Section1:
    '#' name += HEADING '\n'
;

Section2:
    '##' name += HEADING '\n'
;

terminal HEADING: (('A'..'Z') | '_'| ('a'..'z') | ('0'..'9') | '-')* ;

But this gives error as :

The following token definitions can never be matched because prior tokens match the same input: RULE_INT

Also, heading cannot have any special characters with this.

What is correct way of writing this grammar?


Solution

  • instead of using a new terminal rule HEADING, use terminal rule ID which is already defined:

    Section1:
        '#' name = ID '\n'
    ;
    
    Section2:
       '##' name = ID '\n'
    ;