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