-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyparser_cpp.py
64 lines (48 loc) · 1.65 KB
/
myparser_cpp.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
import os
from myparser_tool import char_maybe, char_any0, char_any1
def str_gen(value):
result = value.encode('string_escape').replace('"', r'\"')
return 'MP_STR("' + result + '")'
indent0 = os.linesep
indent1 = indent0 + ' '
indent1c = ',' + indent1
indent2 = indent1 + ' '
indent2c = ',' + indent2
cplusplus_dump = {
'space': lambda: 'RuleItemSpace<>',
'keyword': lambda x: 'RuleItemKeyword<' + str_gen(x) + '>',
'ref': lambda x: 'RuleItemRef<' + str_gen(x) + '>',
'ref' + char_maybe: lambda x: 'RuleItemRef<' + str_gen(x) + ', TagMaybe>',
'ref' + char_any0: lambda x: 'RuleItemRef<' + str_gen(x) + ', TagAny0>',
'ref' + char_any1: lambda x: 'RuleItemRef<' + str_gen(x) + ', TagAny1>',
'error': lambda x: 'RuleItemError<' + str_gen(x) + '>',
'line': lambda l: (
'RuleLine<' + indent2 + indent2c.join(l) + indent1 + '>'
),
'list': lambda n, l: (
'template<>' + indent0
+ 'class RuleDef<' + str_gen(n) + '>:' + indent0
+ 'public RuleList<' + str_gen(n)
+ indent1c + indent1c.join(l) + indent0
+ '> {};' + indent0
),
'regex': lambda n, x: (
'template<>' + indent0
+ 'class RuleDef<' + str_gen(n) + '>:' + indent0
+ 'public RuleRegex<' + str_gen(n)
+ indent1c + str_gen(x) + indent0
+ '> {};' + indent0
)
}
def cplusplus_gen(content, mppath):
return (
'''// generated by MyParser C++ Code Generator
#pragma once
#include "''' + mppath + '''myparser.hpp"
namespace myparser {
''' + content + '''
}
'''
)
def cplusplus_gen_auto(parser, mppath):
return cplusplus_gen(parser.xdump(cplusplus_dump), mppath)