|
| 1 | +/** |
| 2 | + * Treemap grammar for Langium |
| 3 | + * Converted from mindmap grammar |
| 4 | + * |
| 5 | + * The ML_COMMENT and NL hidden terminals handle whitespace, comments, and newlines |
| 6 | + * before the treemap keyword, allowing for empty lines and comments before the |
| 7 | + * treemap declaration. |
| 8 | + */ |
| 9 | +grammar TreemapGrammar |
| 10 | + |
| 11 | +fragment TitleAndAccessibilities: |
| 12 | + ((accDescr=ACC_DESCR | accTitle=ACC_TITLE | title=TITLE))+ |
| 13 | +; |
| 14 | + |
| 15 | +terminal BOOLEAN returns boolean: 'true' | 'false'; |
| 16 | + |
| 17 | +terminal ACC_DESCR: /[\t ]*accDescr(?:[\t ]*:([^\n\r]*?(?=%%)|[^\n\r]*)|\s*{([^}]*)})/; |
| 18 | +terminal ACC_TITLE: /[\t ]*accTitle[\t ]*:(?:[^\n\r]*?(?=%%)|[^\n\r]*)/; |
| 19 | +terminal TITLE: /[\t ]*title(?:[\t ][^\n\r]*?(?=%%)|[\t ][^\n\r]*|)/; |
| 20 | + |
| 21 | +// Interface declarations for data types |
| 22 | +interface Item { |
| 23 | + name: string |
| 24 | + classSelector?: string // For ::: class |
| 25 | +} |
| 26 | +interface Section extends Item { |
| 27 | +} |
| 28 | +interface Leaf extends Item { |
| 29 | + value: number |
| 30 | +} |
| 31 | +interface ClassDefStatement { |
| 32 | + className: string |
| 33 | + styleText: string // Optional style text |
| 34 | +} |
| 35 | +interface Treemap { |
| 36 | + TreemapRows: TreemapRow[] |
| 37 | + title?: string |
| 38 | + accTitle?: string |
| 39 | + accDescr?: string |
| 40 | +} |
| 41 | + |
| 42 | +entry Treemap returns Treemap: |
| 43 | + TREEMAP_KEYWORD |
| 44 | + ( |
| 45 | + TitleAndAccessibilities |
| 46 | + | TreemapRows+=TreemapRow |
| 47 | + )*; |
| 48 | +terminal TREEMAP_KEYWORD: 'treemap-beta' | 'treemap'; |
| 49 | + |
| 50 | +terminal CLASS_DEF: /classDef\s+([a-zA-Z_][a-zA-Z0-9_]+)(?:\s+([^;\r\n]*))?(?:;)?/; |
| 51 | +terminal STYLE_SEPARATOR: ':::'; |
| 52 | +terminal SEPARATOR: ':'; |
| 53 | +terminal COMMA: ','; |
| 54 | + |
| 55 | +hidden terminal WS: /[ \t]+/; // One or more spaces or tabs for hidden whitespace |
| 56 | +hidden terminal ML_COMMENT: /\%\%[^\n]*/; |
| 57 | +hidden terminal NL: /\r?\n/; |
| 58 | + |
| 59 | +TreemapRow: |
| 60 | + indent=INDENTATION? (item=Item | ClassDef); |
| 61 | + |
| 62 | +// Class definition statement handled by the value converter |
| 63 | +ClassDef returns string: |
| 64 | + CLASS_DEF; |
| 65 | + |
| 66 | +Item returns Item: |
| 67 | + Leaf | Section; |
| 68 | + |
| 69 | +// Use a special rule order to handle the parsing precedence |
| 70 | +Section returns Section: |
| 71 | + name=STRING2 (STYLE_SEPARATOR classSelector=ID2)?; |
| 72 | + |
| 73 | +Leaf returns Leaf: |
| 74 | + name=STRING2 INDENTATION? (SEPARATOR | COMMA) INDENTATION? value=MyNumber (STYLE_SEPARATOR classSelector=ID2)?; |
| 75 | + |
| 76 | +// This should be processed before whitespace is ignored |
| 77 | +terminal INDENTATION: /[ \t]{1,}/; // One or more spaces/tabs for indentation |
| 78 | + |
| 79 | +// Keywords with fixed text patterns |
| 80 | +terminal ID2: /[a-zA-Z_][a-zA-Z0-9_]*/; |
| 81 | +// Define as a terminal rule |
| 82 | +terminal NUMBER2: /[0-9_\.\,]+/; |
| 83 | + |
| 84 | +// Then create a data type rule that uses it |
| 85 | +MyNumber returns number: NUMBER2; |
| 86 | + |
| 87 | +terminal STRING2: /"[^"]*"|'[^']*'/; |
| 88 | +// Modified indentation rule to have higher priority than WS |
0 commit comments