djowel

Untitled

Nov 7th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.80 KB | None | 0 0
  1. {- YAML Grammar -}
  2. %Namespace = QiHe.Yaml.Grammar;
  3. %ParserName = YamlParser;
  4.  
  5. {- Yaml Document -}
  6.  
  7. YamlStream
  8. <- Comment* Documents:(ImplicitDocument? ExplicitDocument*) <end>;
  9.  
  10. YamlDocument ImplicitDocument
  11. <- {currentDocument = yamlDocument; currentIndent = -1;}
  12. Root:IndentedBlockNode EndOfDocument?;
  13.  
  14. YamlDocument ExplicitDocument
  15. <- {currentDocument = yamlDocument; currentIndent = -1;}
  16. Directives:Directive* '---' Root:SeparatedBlockNode EndOfDocument?;
  17.  
  18. void EndOfDocument
  19. <- '...' InlineComments;
  20.  
  21. {- Directives -}
  22.  
  23. Directive
  24. <- YamlDirective
  25. / TagDirective
  26. / ReservedDirective
  27. ;
  28.  
  29. ReservedDirective
  30. <- '%' Name:DirectiveName Parameters:(SeparationSpace $DirectiveParameter)* InlineComments;
  31.  
  32. string DirectiveName
  33. <- $NonSpaceChar+;
  34.  
  35. string DirectiveParameter
  36. <- $NonSpaceChar+;
  37.  
  38. YamlDirective
  39. <- 'YAML' SeparationSpace Version:YamlVersion InlineComments;
  40.  
  41. YamlVersion
  42. <- Major:Integer '.' Minor:Integer;
  43.  
  44. TagDirective
  45. <- 'TAG' SeparationSpace Handle:TagHandle SeparationSpace Prefix:TagPrefix InlineComments;
  46.  
  47. TagHandle
  48. <- NamedTagHandle
  49. / SecondaryTagHandle
  50. / PrimaryTagHandle
  51. ;
  52.  
  53. PrimaryTagHandle
  54. <- '!';
  55.  
  56. SecondaryTagHandle
  57. <- '!!';
  58.  
  59. NamedTagHandle
  60. <- '!' Name:WordChar+ '!';
  61.  
  62. TagPrefix
  63. <- LocalTagPrefix
  64. / GlobalTagPrefix
  65. ;
  66.  
  67. LocalTagPrefix
  68. <- '!' Prefix:UriChar*;
  69.  
  70. GlobalTagPrefix
  71. <- Prefix:UriChar+;
  72.  
  73. {- Inheritance relation -}
  74.  
  75. DataItem {- Just for generate type inheritance -}
  76. <- (Property:NodeProperty SeparationLines)? (Scalar / Sequence / Mapping);
  77.  
  78. Scalar
  79. <- FlowScalarInBlock / FlowScalarInFlow / BlockScalar;
  80.  
  81. Sequence
  82. <- FlowSequence / BlockSequence;
  83.  
  84. Mapping
  85. <- FlowMapping / BlockMapping;
  86.  
  87. {- Node structure -}
  88.  
  89. DataItem IndentedBlockNode
  90. <- { IncreaseIndent(); } IndentedBlock @{ DecreaseIndent(); };
  91.  
  92. DataItem SeparatedBlockNode
  93. <- { IncreaseIndent(); } SeparatedBlock @{ DecreaseIndent(); };
  94.  
  95. DataItem IndentedBlock
  96. <- IndentedContent
  97. / Indent AliasNode InlineComments
  98. / Indent property:NodeProperty SeparatedContent? { SetDataItemProperty(dataItem, property); }
  99. ;
  100.  
  101. DataItem SeparatedBlock
  102. <- SeparatedContent
  103. / SeparationLines AliasNode InlineComments
  104. / SeparationSpace property:NodeProperty SeparatedContent?{ SetDataItemProperty(dataItem, property); }
  105. / EmptyBlock
  106. ;
  107.  
  108. DataItem IndentedContent
  109. <- Indent BlockContent
  110. / Indent FlowContentInBlock InlineComments
  111. ;
  112.  
  113. DataItem SeparatedContent
  114. <- InlineComments IndentedContent
  115. / SeparationSpace BlockScalar
  116. / SeparationSpace FlowContentInBlock InlineComments
  117. ;
  118.  
  119. DataItem BlockCollectionEntry
  120. <- { IncreaseIndent(); } (SeparationSpaceAsIndent BlockCollection) @{ DecreaseIndent(); }
  121. / SeparatedBlockNode
  122. ;
  123.  
  124. DataItem BlockCollectionEntryOptionalIndent
  125. <- { RememberIndent(); } (SeparationSpaceAsIndent BlockCollection) @{ RestoreIndent(); }
  126. / { RememberIndent(); } SeparatedBlock @{ RestoreIndent(); }
  127. ;
  128.  
  129. DataItem FlowNodeInFlow
  130. <- AliasNode
  131. / FlowContentInFlow
  132. / property:NodeProperty {dataItem = new Scalar();} (SeparationLinesInFlow FlowContentInFlow)? { SetDataItemProperty(dataItem, property); }
  133. ;
  134.  
  135. DataItem AliasNode
  136. <- '*' name:AnchorName { return GetAnchoredDataItem(name); };
  137.  
  138. DataItem FlowContentInBlock
  139. <- FlowScalarInBlock
  140. / FlowSequence
  141. / FlowMapping;
  142.  
  143. DataItem FlowContentInFlow
  144. <- FlowScalarInFlow
  145. / FlowSequence
  146. / FlowMapping;
  147.  
  148. DataItem BlockContent
  149. <- BlockScalar
  150. / BlockSequence
  151. / BlockMapping;
  152.  
  153. DataItem BlockCollection
  154. <- BlockSequence
  155. / BlockMapping;
  156.  
  157. DataItem EmptyFlow <- <empty> { return new Scalar(); };
  158. DataItem EmptyBlock <- EmptyFlow InlineComments;
  159.  
  160. {- Node Property -}
  161.  
  162. NodeProperty <- Tag:Tag (SeparationLines Anchor:Anchor)? / Anchor:Anchor (SeparationLines Tag:Tag)?;
  163.  
  164. string Anchor
  165. <- '&' $AnchorName;
  166.  
  167. string AnchorName
  168. <- $NonSpaceChar+;
  169.  
  170. Tag
  171. <- VerbatimTag
  172. / ShorthandTag
  173. / NonSpecificTag
  174. ;
  175.  
  176. NonSpecificTag
  177. <- '!';
  178.  
  179. VerbatimTag
  180. <- '!' '<' Chars:UriChar+ '>';
  181.  
  182. ShorthandTag
  183. <- NamedTagHandle Chars:TagChar+
  184. / SecondaryTagHandle Chars:TagChar+
  185. / PrimaryTagHandle Chars:TagChar+
  186. ;
  187.  
  188. {- Scalar -}
  189.  
  190. Scalar FlowScalarInBlock
  191. <- Text:PlainTextMultiLine / Text:SingleQuotedText / Text:DoubleQuotedText;
  192.  
  193. Scalar FlowScalarInFlow
  194. <- Text:PlainTextInFlow / Text:SingleQuotedText / Text:DoubleQuotedText;
  195.  
  196. Scalar BlockScalar
  197. <- Text:LiteralText / Text:FoldedText;
  198.  
  199. {- Plain Text -}
  200.  
  201. string PlainText <- $PlainTextMultiLine / $PlainTextInFlow;
  202.  
  203. string PlainTextMultiLine <- $PlainTextSingleLine $PlainTextMoreLine*;
  204.  
  205. string PlainTextSingleLine <- !DocumentMarker $PlainTextFirstChar $(PlainTextChar / SpacedPlainTextChar)*;
  206.  
  207. string PlainTextMoreLine <- IgnoredBlank $LineFolding Indent IgnoredSpace $(PlainTextChar / SpacedPlainTextChar)+;
  208.  
  209. string PlainTextInFlow <- $PlainTextInFlowSingleLine $PlainTextInFlowMoreLine*;
  210.  
  211. string PlainTextInFlowSingleLine <- !DocumentMarker $PlainTextFirstCharInFlow $(PlainTextCharInFlow / SpacedPlainTextCharInFlow)*;
  212.  
  213. string PlainTextInFlowMoreLine <- IgnoredBlank $LineFolding Indent IgnoredSpace $(PlainTextCharInFlow / SpacedPlainTextCharInFlow)+;
  214.  
  215. string PlainTextFirstChar <- $-"\r\n\t -?:,[]{}#&*!|>'\"%@`" / $"-?:" $NonSpaceChar;
  216.  
  217. string PlainTextChar <- $':' $NonSpaceChar / $NonSpaceChar $'#'+ / { text.Length = 0; } ch2:-"\r\n\t :#";
  218.  
  219. string SpacedPlainTextChar <- $' '+ $PlainTextChar;
  220.  
  221. string PlainTextFirstCharInFlow <- $-"\r\n\t -?:,[]{}#&*!|>'\"%@`"
  222. / $"-?:" $NonSpaceSep;
  223.  
  224. string PlainTextCharInFlow <- $":" $NonSpaceSep / $NonSpaceSep $'#' / { text.Length = 0; } ch2:-"\r\n\t :#,[]{}";
  225.  
  226. string SpacedPlainTextCharInFlow <- $' '+ $PlainTextCharInFlow;
  227.  
  228. void DocumentMarker <- <sol> '---' (Space/LineBreak)
  229. / <sol> '...' (Space/LineBreak);
  230.  
  231. {- Quoted Text -}
  232.  
  233. string DoubleQuotedText <- $DoubleQuotedSingleLine / $DoubleQuotedMultiLine;
  234.  
  235. string DoubleQuotedSingleLine <- '"' $(-"\"\\\r\n" / EscapeSequence)* '"';
  236.  
  237. string DoubleQuotedMultiLine <- $DoubleQuotedMultiLineFist $DoubleQuotedMultiLineInner* $DoubleQuotedMultiLineLast;
  238.  
  239. string DoubleQuotedMultiLineFist <- '"' $(-" \"\\\r\n" / EscapeSequence / ' ' !(IgnoredBlank LineBreak))* IgnoredBlank $DoubleQuotedMultiLineBreak;
  240.  
  241. string DoubleQuotedMultiLineInner <- Indent IgnoredBlank $(-" \"\\\r\n" / EscapeSequence / ' ' !(IgnoredBlank LineBreak))+ IgnoredBlank $DoubleQuotedMultiLineBreak;
  242.  
  243. string DoubleQuotedMultiLineLast <- Indent IgnoredBlank $(-"\"\\\r\n" / EscapeSequence)* '"';
  244.  
  245. string DoubleQuotedMultiLineBreak <- $LineFolding / EscapedLineBreak;
  246.  
  247. string SingleQuotedText <- $SingleQuotedSingleLine / $SingleQuotedMultiLine;
  248.  
  249. string SingleQuotedSingleLine <- '\'' $(-"'\r\n" / EscapedSingleQuote)* '\'';
  250.  
  251. string SingleQuotedMultiLine <- $SingleQuotedMultiLineFist $SingleQuotedMultiLineInner* $SingleQuotedMultiLineLast;
  252.  
  253. string SingleQuotedMultiLineFist <- '\'' $(-" '\r\n" / EscapedSingleQuote / ' ' !(IgnoredBlank LineBreak))* IgnoredBlank fold:LineFolding;
  254.  
  255. string SingleQuotedMultiLineInner <- Indent IgnoredBlank $(-" '\r\n" / EscapedSingleQuote / ' ' !(IgnoredBlank LineBreak))+ IgnoredBlank fold:LineFolding;
  256.  
  257. string SingleQuotedMultiLineLast <- Indent IgnoredBlank $(-"'\r\n" / EscapedSingleQuote)* '\'';
  258.  
  259. string LineFolding <- $ReservedLineBreak (IgnoredBlank LineBreak)+ &Indent / LineBreak &Indent {return " ";};
  260.  
  261. char EscapedSingleQuote <- { char ch = default(char); } '\'\'' { return '\''; };
  262.  
  263. void EscapedLineBreak <- '\\' LineBreak (IgnoredBlank LineBreak)*;
  264.  
  265. {- Block Text -}
  266.  
  267. string LiteralText <- '|' (modifier:BlockScalarModifier)? @{AddIndent(modifier, success);} InlineComment $LiteralContent? @{DecreaseIndent();};
  268.  
  269. string FoldedText <- '>' (modifier:BlockScalarModifier)? @{AddIndent(modifier, success);} InlineComment ((str_2:$EmptyLineBlock)* $FoldedLines $ChompedLineBreak Comments?) @{DecreaseIndent();};
  270.  
  271. BlockScalarModifier <- Indent:IndentIndicator (Chomp:ChompingIndicator)? / Chomp:ChompingIndicator (Indent:IndentIndicator)?;
  272.  
  273. string LiteralContent <- $LiteralFirst $LiteralInner* str2:ChompedLineBreak Comments?;
  274. string LiteralFirst <- $EmptyLineBlock* Indent $NonBreakChar+;
  275. string LiteralInner <- $ReservedLineBreak $EmptyLineBlock* Indent $NonBreakChar+;
  276.  
  277. string FoldedLine <- Indent $NonBreakChar*;
  278. string FoldedLines <- str2:FoldedLine $(LineFolding FoldedLine)*;
  279.  
  280. string SpacedLine <- Indent Blank $NonBreakChar*;
  281. string SpacedLines <- str2:SpacedLine (LineBreak $SpacedLine)*;
  282.  
  283. char IndentIndicator <- $'1'...'9';
  284.  
  285. char ChompingIndicator <- $'-' / $'+';
  286.  
  287. {- Sequence -}
  288.  
  289. Sequence FlowSequence
  290. <- '[' SeparationLinesInFlow? Enties:(FlowSequenceEntry (',' SeparationLinesInFlow? FlowSequenceEntry)*) ']';
  291.  
  292. DataItem FlowSequenceEntry
  293. <- FlowNodeInFlow SeparationLinesInFlow?
  294. / FlowSingPair
  295. ;
  296.  
  297. Sequence BlockSequence
  298. <- Enties:(BlockSequenceEntry (Indent BlockSequenceEntry)*);
  299.  
  300. DataItem BlockSequenceEntry
  301. <- '-' BlockCollectionEntry;
  302.  
  303. {- Mapping -}
  304.  
  305. Mapping FlowMapping
  306. <- '{' SeparationLinesInFlow? Enties:(FlowMappingEntry (',' SeparationLinesInFlow? FlowMappingEntry)*) '}';
  307.  
  308. MappingEntry FlowMappingEntry
  309. <- Key:ExplicitKey Value:ExplicitValue
  310. / Key:ExplicitKey Value:EmptyFlow
  311. / Key:SimpleKey Value:ExplicitValue
  312. / Key:SimpleKey Value:EmptyFlow
  313. ;
  314.  
  315. DataItem ExplicitKey
  316. <- '?' SeparationLinesInFlow FlowNodeInFlow SeparationLinesInFlow?
  317. / '?' EmptyFlow SeparationLinesInFlow
  318. ;
  319.  
  320. DataItem SimpleKey
  321. <- FlowKey SeparationLinesInFlow?;
  322.  
  323. Scalar FlowKey
  324. <- Text:PlainTextInFlowSingleLine
  325. / Text:DoubleQuotedSingleLine
  326. / Text:SingleQuotedSingleLine
  327. ;
  328.  
  329. Scalar BlockKey
  330. <- Text:PlainTextSingleLine
  331. / Text:DoubleQuotedSingleLine
  332. / Text:SingleQuotedSingleLine
  333. ;
  334.  
  335. DataItem ExplicitValue
  336. <- ':' SeparationLinesInFlow FlowNodeInFlow SeparationLinesInFlow?
  337. / ':' EmptyFlow SeparationLinesInFlow
  338. ;
  339.  
  340. MappingEntry FlowSingPair
  341. <- Key:ExplicitKey Value:ExplicitValue
  342. / Key:ExplicitKey Value:EmptyFlow
  343. / Key:SimpleKey Value:ExplicitValue
  344. ;
  345.  
  346. Mapping BlockMapping
  347. <- Enties:(BlockMappingEntry (Indent BlockMappingEntry)*);
  348.  
  349. MappingEntry BlockMappingEntry
  350. <- Key:BlockExplicitKey Value:BlockExplicitValue
  351. / Key:BlockExplicitKey Value:EmptyFlow
  352. / Key:BlockSimpleKey Value:BlockSimpleValue
  353. / Key:BlockSimpleKey Value:EmptyBlock
  354. ;
  355.  
  356. DataItem BlockExplicitKey
  357. <- '?' BlockCollectionEntry;
  358.  
  359. DataItem BlockExplicitValue
  360. <- Indent ':' BlockCollectionEntry;
  361.  
  362. DataItem BlockSimpleKey
  363. <- BlockKey SeparationLines? ':';
  364.  
  365. DataItem BlockSimpleValue
  366. <- BlockCollectionEntry;
  367.  
  368. {- Comment -}
  369.  
  370. void Comment
  371. <- !<eof> IgnoredSpace ('#' NonBreakChar*)? (LineBreak / <eof>);
  372.  
  373. void InlineComment
  374. <- (SeparationSpace ('#' NonBreakChar*)?)? (LineBreak / <eof>);
  375.  
  376. void Comments
  377. <- Comment+;
  378.  
  379. void InlineComments
  380. <- InlineComment Comment*;
  381.  
  382.  
  383. string Integer <- chars:Digit+ { return new string(chars.ToArray()); };
  384.  
  385. char WordChar <- $Letter / $Digit / $'-';
  386.  
  387. char Letter <- $'a'...'z' / $'A'...'Z';
  388.  
  389. char Digit <- $'0'...'9';
  390.  
  391. char HexDigit <- $'0'...'9' / $'A'...'F' / $'a'...'f';
  392.  
  393. char UriChar
  394. <- $WordChar
  395. / '%' char1:HexDigit char2:HexDigit { ch = Convert.ToChar(int.Parse(String.Format("{0}{1}", char1, char2), System.Globalization.NumberStyles.HexNumber));}
  396. / ";/?:@&=+$,_.!~*'()[]";
  397.  
  398. char TagChar
  399. <- $WordChar
  400. / '%' char1:HexDigit char2:HexDigit { ch = Convert.ToChar(int.Parse(String.Format("{0}{1}", char1, char2), System.Globalization.NumberStyles.HexNumber));}
  401. / ";/?:@&=+$,_.~*'()[]";
  402.  
  403. void EmptyLinePlain <- IgnoredSpace NormalizedLineBreak;
  404.  
  405. void EmptyLineQuoted <- IgnoredBlank NormalizedLineBreak;
  406.  
  407. string EmptyLineBlock <- IgnoredSpace $ReservedLineBreak;
  408.  
  409. char NonSpaceChar <- -" \t\r\n";
  410. char NonSpaceSep <- -"\r\n\t ,[]{}";
  411.  
  412. char NonBreakChar <- -"\r\n";
  413.  
  414. void IgnoredSpace <- ' '*;
  415. void IgnoredBlank <- " \t"*;
  416. void SeparationSpace <- ' '+;
  417. void SeparationLines <- InlineComments Indent / SeparationSpace;
  418. void SeparationLinesInFlow <- InlineComments {detectIndent = false;} Indent IgnoredSpace / SeparationSpace;
  419.  
  420. void SeparationSpaceAsIndent
  421. <- (' ' { currentIndent++; })+;
  422.  
  423. void Indent <- { success = ParseIndent(); };
  424.  
  425. char Space <- ' ';
  426. char Blank <- " \t";
  427. void LineBreak <- '\r\n' / '\r' / '\n';
  428. string ReservedLineBreak <- $'\r\n' / $'\r' / $'\n';
  429. string ChompedLineBreak <- ($ReservedLineBreak (IgnoredSpace $ReservedLineBreak)* / <eof>) @{ return Chomp(text.ToString()); };
  430.  
  431. char NormalizedLineBreak <- { char ch = default(char); } LineBreak { return '\n'; };
  432.  
  433. char EscapeSequence
  434. <- '\\\\' { return '\\'; }
  435. / '\\\'' { return '\''; }
  436. / '\\"' { return '\"'; }
  437. / '\\r' { return '\r'; }
  438. / '\\n' { return '\n'; }
  439. / '\\t' { return '\t'; }
  440. / '\\v' { return '\v'; }
  441. / '\\a' { return '\a'; }
  442. / '\\b' { return '\b'; }
  443. / '\\f' { return '\f'; }
  444. / '\\0' { return '\0'; }
  445. / '\\/' { return '/'; }
  446. / '\\ ' { return ' '; }
  447. / '\\ ' { return ' '; }
  448. / '\\_' { return '\u00A0'; }
  449. / '\\e' { return '\u001B'; }
  450. / '\\N' { return '\u0085'; }
  451. / '\\L' { return '\u2028'; }
  452. / '\\P' { return '\u2029'; }
  453. / '\\x' char1:HexDigit char2:HexDigit { return Convert.ToChar(int.Parse(String.Format("{0}{1}", char1, char2), System.Globalization.NumberStyles.HexNumber));}
  454. / '\\u' char1:HexDigit char2:HexDigit char3:HexDigit char4:HexDigit { return Convert.ToChar(int.Parse(String.Format("{0}{1}{2}{3}", char1, char2, char3, char4), System.Globalization.NumberStyles.HexNumber));}
  455. ;
  456.  
Advertisement
Add Comment
Please, Sign In to add comment