>XText for RelaxNG

>So, I decided to try and create an XText Grammar for the RelaxNG Compact Syntax. Mixed results so far, mainly having to do with my lack of knowledge on the XText grammar and it’s mapping to EBNF.

Here is what I have so far:


grammar org.oasisopen.relaxng with org.eclipse.xtext.common.Terminals

generate relaxng "http://www.oasis-open.org/relaxng"

terminal Letter:
ID;

terminal CHAR:
'#x9' | '#xA' | '#xD' | ('#x20'..'#xD7FF') | ('#xE000'..'#xFFFD') | ('#x10000'..'#x10FFFF');

terminal NewLine:
'#xA' | '#xD' | ('#xA' '#xD');

terminal NameStartChar:
":" |
('A'..'Z') |
"_" |
('a'..'z') |
('#xC0'..'#xD6') |
('#xD8'..'#xF6') |
('#xF8'..'#x2FF') |
('#x370'..'#x37D') |
('#x37F'..'#x1FFF') |
('#x200C'..'#x200D') |
('#x2070'..'#x218F') |
('#x2C00'..'#x2FEF') |
('#x3001'..'#xD7FF') |
('#xF900'..'#xFDCF') |
('[#xFDF0'..'-#xFFFD') |
('#x10000'..'#xEFFFF');

terminal NameChar:
NameStartChar | "-" | "." | ('0'..'9') | '#xB7' | ('#x0300'..'#x036F') | ('#x203F'..'#x2040');

Model :
(elements += TopLevel);

TopLevel:
Decl* (Pattern | GrammarContent*);

Decl:
'namespace' (IdentifierOrKeyWord '=' NamespaceURILiteral) |
'default' 'namespace' (IdentifierOrKeyWord) '=' NamespaceURILiteral |
'datatypes' IdentifierOrKeyWord '=' Literal;

Pattern:
'element' NameClass '{' Pattern '}' |
'attribute' NameClass '{' Pattern '}' |
Pattern (',' Pattern)+ |
Pattern ('&' Pattern)+ |
Pattern ('|' Pattern)+ |
Pattern '?' |
Pattern '*' |
Pattern '+' |
'list' '{' Pattern '}' |
'mixed' '{' Pattern '}' |
Identifier |
'parent' Identifier |
'empty' |
'text' |
(DataTypeName) DataTypeValue |
DataTypeName ('{' Param* '}') (ExceptPattern) |
'notAllowed' |
'external' AnyURILiteral (Inherit) |
'grammar' '{' GrammarContent* '}' |
'(' Pattern ')';

Param:
IdentifierOrKeyWord '=' Literal;

ExceptPattern:
'-' Pattern;

GrammarContent:
Start | Define |
'div' '{' GrammarContent* '}' |
'include' AnyURILiteral (Inherit) ('{' IncludeContent* '}');

IncludeContent:
Define | Start |
'div' '{' GrammarContent* '}';

Start:
'start' AssignedMethod Pattern;

Define:
Identifier AssignedMethod Pattern;

AssignMethod:
'=' |
'|=' |
'&=';

NameClass:
Name |
NsName (ExceptClassName) |
AnyName (ExceptClassName) |
NameClass '|' NameClass |
'(' NameClass ')';

Name:
IdentiferOrKeyWord | CName;

ExceptNameClass:
'-' NameClass;

DataTypeName:
CName |
'string' |
'token';

DataTypeValue:
Literal;

AnyURILiteral:
Literal;

NamespaceURILiteral:
Literal | 'inherit';

Inherit:
'inherit' '=' IdentifierOrKeyWord;

IdentifierOrKeyWord:
Identifier | KeyWord;


Identifier:
(NCName .. KeyWord) |
QuotedIdentifier;


CName:
NCName ':' NCName;

NsName:
NCName ':*';

AnyName:
'*';

Literal:
LiteralSegment ('~' LiteralSegment)+;

LiteralSegment:
'"' (CHAR .. ('"' | NewLine))* '"' |
"'" (CHAR .. ("'" | Newline))* "'" |
'"""' (('"') ('"') (CHAR .. '"'))* '"""';

KeyWord:
"attribute"
| "default"
| "datatypes"
| "div"
| "element"
| "empty"
| "external"
| "grammar"
| "include"
| "inherit"
| "list"
| "mixed"
| "namespace"
| "notAllowed"
| "parent"
| "start"
| "string"
| "text"
| "token";

NCName:
NCNameStartChar NCNameChar*; /* An XML Name, minus the ":" */

NCNameChar:
NameChar - ':';

NCNameStartChar:
Letter | '_';

Name:
NameStartChar (NameChar)*;

Names:
Name ('#x20' Name)*;

Nmtoken:
(NameChar)+;

Nmtokens:
Nmtoken ('#x20' Nmtoken)*;

QuotedIdentifier:
'\' NCName;

Some of the errors I can figure out…others are just baffling. Particular the ones where it can’t find the Rule even though it’s been editted. Help from the XText gurus out their would be appreciated.

Advertisements
This entry was posted in eclipse, relaxng. Bookmark the permalink.

One Response to >XText for RelaxNG

  1. >Hi David,the grammar validation tries to give you some hints about what may not be valid. For example, you cannot write a rule like:TopLevel: Decl* (Pattern | GrammarContent*);because it is not possible to use unassigned rule calls (e.g. the call of 'Decl') with a cardinality that is not default or optional but a loop operator (*).As I don't know much about relaxng, you may email me directly and we can chat a little bit about the semantics. I guess we'll figure out what's wrong and what may work instead.Regards,Sebastian

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s