-
Notifications
You must be signed in to change notification settings - Fork 23
/
bubble-to-pegjs.rb
executable file
·150 lines (119 loc) · 2.48 KB
/
bubble-to-pegjs.rb
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
#!/usr/bin/env ruby
$grammar = ARGV[0]
$grammarx = ARGV[1] || 'bubble-to-pegjs_ex.rb'
$keywords = {}
$override = {}
# ----------------------------------------
def rule(name, *rest)
print "#{name} =\n"
if $override[name]
print "#{$override[name]}\n"
else
rest.flatten.each do |x|
$keywords[x] = true if x.class == String and x.match(/^[A-Z]+$/)
print "#{x} "
end
print "\n\n"
end
end
def either(*rest)
r = []
rest.each {|x| r << x; r << '/' }
["("] + r[0..-2] + [")"]
end
def stack(*rest)
return rest if rest.length <= 1
return ["("] + rest + [")"]
end
def line(*rest)
return stack(*rest)
end
def loop(*rest)
return ["("] + rest + [")+"]
end
def toploop(*rest)
return loop(*rest)
end
def tailbranch(*rest) # TODO.
return rest
end
def opt(*rest)
return ["("] + rest + [")?"]
end
def optx(*rest)
return opt(*rest)
end
# ----------------------------------------
$header = <<-EOS
// generated pegjs, from #{$grammar} and #{$grammarx}
start = sql_stmt_list
EOS
# ----------------------------------------
$extra = <<-EOS
dot = '.'
comma = ','
semicolon = ';'
minusminus = '--'
minus = '-'
plus = '+'
lparen = '('
rparen = ')'
star = '*'
newline = '\\n'
anything_except_newline = [^\\n]*
comment_beg = '/*'
comment_end = '*/'
anything_except_comment_end = .* & '*/'
string_literal = '\"' (escape_char / [^"])* '\"'
escape_char = '\\\\' .
nil = ''
whitespace = [\\s]*
whitespace1 = [\\s]+
unary_operator = '-' / '+' / '~' / 'NOT'
binary_operator =
'||'
/ '*' / '/' / '%'
/ '+' / '-'
/ '<<' / '>>' / '&' / '|'
/ '<' / '<=' / '>' / '>='
/ '=' / '==' / '!=' / '<>'
/ 'IS' / 'IS NOT' / 'IN' / 'LIKE' / 'GLOB' / 'MATCH' / 'REGEXP'
/ 'AND'
/ 'OR'
digit = [0-9]
decimal_point = dot
equal = '='
name = [A-Za-z0-9_]+
database_name = name
table_name = name
table_alias = name
table_or_index_name = name
new_table_name = name
index_name = name
column_name = name
column_alias = name
foreign_table = name
savepoint_name = name
collation_name = name
trigger_name = name
view_name = name
module_name = name
module_argument = name
bind_parameter = name
function_name = name
pragma_name = name
error_message = string_literal
CURRENT_TIME = 'now'
CURRENT_DATE = 'now'
CURRENT_TIMESTAMP = 'now'
blob_literal = string_literal
end_of_input = ''
EOS
# ----------------------------------------
load $grammarx
print "#{$header}\n"
load $grammar
print "#{$extra}\n"
$keywords.keys.sort.each do |keyword|
print "#{keyword} = whitespace1 \"#{keyword}\"\n"
end