forked from JetBrains/kotlin-web-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.py
93 lines (79 loc) · 2.78 KB
/
grammar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import os.path as path
from xml.etree.ElementTree import ElementTree
from src.markdown.makrdown import customized_markdown
def _get_description(node):
description = {
'type': 'description',
'content': []
}
for children in node:
token = {}
if children.tag == 'whiteSpace':
token['type'] = 'whitespace'
token['content'] = children.text
elif children.tag == 'identifier':
token['type'] = 'identifier'
token['name'] = children.attrib['name']
elif children.tag == 'string' or children.tag == 'symbol' or children.tag == 'other':
token['type'] = children.tag
token['content'] = children.text
if children.tag == 'string':
token['content'] = token['content'].replace('<', '<').replace('>', '>')
elif 'crlf':
token['type'] = 'crlf'
description['content'].append(token)
return description
def _get_declaration(node):
declaration = {
'type': 'declaration',
'name': node.attrib['name'],
'usages': []
}
for usage in node.findall('usages'):
for children in usage:
declaration['usages'].append({
'name': children.text
})
return declaration
def _get_item_content(node):
item = {
'type': 'item',
'content': []
}
for children in node:
if children.tag == 'annotation':
item['content'].append({
'type': 'annotation',
'content': children.text
})
elif children.tag == 'declaration':
item['content'].append(_get_declaration(children))
elif children.tag == 'description':
item['content'].append(_get_description(children))
return item
def get_grammar(build_mode: bool):
data = []
grammar_file = path.join(path.dirname(path.dirname(__file__)), 'grammar.xml')
if not os.path.isfile(grammar_file):
if build_mode:
raise FileNotFoundError("Grammar file %s not found" % grammar_file)
else:
print("Grammar page is NOT included. No file ", grammar_file)
return []
grammar_xml = ElementTree(file=grammar_file).getroot()
for grammar_set in grammar_xml:
result_set = {
'file-name': grammar_set.attrib['file-name'],
'content': []
}
for node in grammar_set:
if node.tag == 'doc':
result_set['content'].append({
'type': 'comment',
'content': customized_markdown(node.text)
})
elif node.tag == 'item':
result_set['content'].append(_get_item_content(node))
data.append(result_set)
return data