forked from elliotchance/c2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
163 lines (153 loc) · 4.1 KB
/
ast.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bytes"
"regexp"
"strings"
)
type ExpressionRenderer interface {
// TODO: The two arguments returned are the rendered Go and the C type.
// This should be made into an appropriate type.
Render() []string
}
type LineRenderer interface {
RenderLine(out *bytes.Buffer, functionName string, indent int, returnType string)
}
func Parse(line string) interface{} {
nodeName := strings.SplitN(line, " ", 2)[0]
var node interface{}
switch nodeName {
case "AlwaysInlineAttr":
node = parseAlwaysInlineAttr(line)
case "ArraySubscriptExpr":
node = parseArraySubscriptExpr(line)
case "AsmLabelAttr":
node = parseAsmLabelAttr(line)
case "AvailabilityAttr":
node = parseAvailabilityAttr(line)
case "BinaryOperator":
node = parseBinaryOperator(line)
case "BreakStmt":
node = parseBreakStmt(line)
case "BuiltinType":
node = parseBuiltinType(line)
case "CallExpr":
node = parseCallExpr(line)
case "CharacterLiteral":
node = parseCharacterLiteral(line)
case "CompoundStmt":
node = parseCompoundStmt(line)
case "ConditionalOperator":
node = parseConditionalOperator(line)
case "ConstAttr":
node = parseConstAttr(line)
case "ConstantArrayType":
node = parseConstantArrayType(line)
case "CStyleCastExpr":
node = parseCStyleCastExpr(line)
case "DeclRefExpr":
node = parseDeclRefExpr(line)
case "DeclStmt":
node = parseDeclStmt(line)
case "DeprecatedAttr":
node = parseDeprecatedAttr(line)
case "ElaboratedType":
node = parseElaboratedType(line)
case "Enum":
node = parseEnum(line)
case "EnumConstantDecl":
node = parseEnumConstantDecl(line)
case "EnumDecl":
node = parseEnumDecl(line)
case "EnumType":
node = parseEnumType(line)
case "FieldDecl":
node = parseFieldDecl(line)
case "FloatingLiteral":
node = parseFloatingLiteral(line)
case "FormatAttr":
node = parseFormatAttr(line)
case "FunctionDecl":
node = parseFunctionDecl(line)
case "FunctionProtoType":
node = parseFunctionProtoType(line)
case "ForStmt":
node = parseForStmt(line)
case "IfStmt":
node = parseIfStmt(line)
case "ImplicitCastExpr":
node = parseImplicitCastExpr(line)
case "IntegerLiteral":
node = parseIntegerLiteral(line)
case "MallocAttr":
node = parseMallocAttr(line)
case "MemberExpr":
node = parseMemberExpr(line)
case "ModeAttr":
node = parseModeAttr(line)
case "NoThrowAttr":
node = parseNoThrowAttr(line)
case "NonNullAttr":
node = parseNonNullAttr(line)
case "ParenExpr":
node = parseParenExpr(line)
case "ParmVarDecl":
node = parseParmVarDecl(line)
case "PointerType":
node = parsePointerType(line)
case "PredefinedExpr":
node = parsePredefinedExpr(line)
case "QualType":
node = parseQualType(line)
case "Record":
node = parseRecord(line)
case "RecordDecl":
node = parseRecordDecl(line)
case "RecordType":
node = parseRecordType(line)
case "RestrictAttr":
node = parseRestrictAttr(line)
case "ReturnStmt":
node = parseReturnStmt(line)
case "StringLiteral":
node = parseStringLiteral(line)
case "TranslationUnitDecl":
node = parseTranslationUnitDecl(line)
case "Typedef":
node = parseTypedef(line)
case "TypedefDecl":
node = parseTypedefDecl(line)
case "TypedefType":
node = parseTypedefType(line)
case "UnaryOperator":
node = parseUnaryOperator(line)
case "VarDecl":
node = parseVarDecl(line)
case "WhileStmt":
node = parseWhileStmt(line)
case "NullStmt":
node = nil
default:
panic("Unknown node type: '" + line + "'")
}
return node
}
func groupsFromRegex(rx, line string) map[string]string {
// We remove tabs and newlines from the regex. This is purely cosmetic,
// as the regex input can be quite long and it's nice for the caller to
// be able to format it in a more readable way.
fullRegexp := "(?P<address>[0-9a-fx]+) " +
strings.Replace(strings.Replace(rx, "\n", "", -1), "\t", "", -1)
re := regexp.MustCompile(fullRegexp)
match := re.FindStringSubmatch(line)
if len(match) == 0 {
panic("could not match regexp '" + fullRegexp +
"' with string '" + line + "'")
}
result := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 {
result[name] = match[i]
}
}
return result
}