Skip to content

Commit 3865592

Browse files
committed
bubble-to-pegjs.rb first attempt
1 parent cf35d7f commit 3865592

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tmp

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
all:
2+
./sql-bubble.rb > tmp/rules.rb
3+
./bubble-to-pegjs.rb tmp/rules.rb > tmp/sql.pegjs

bubble-to-pegjs.rb

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env ruby
2+
3+
$keywords = {}
4+
5+
def rule(name, *rest)
6+
print "#{name} =\n"
7+
rest.flatten.each do |x|
8+
$keywords[x] = true if x.class == String and x.match(/^[A-Z]+$/)
9+
print "#{x} "
10+
end
11+
print "\n\n"
12+
end
13+
14+
def either(*rest)
15+
r = []
16+
rest.each {|x| r << x; r << '/' }
17+
["("] + r[0..-2] + [")"]
18+
end
19+
20+
def stack(*rest)
21+
return rest if rest.length <= 1
22+
return ["("] + rest + [")"]
23+
end
24+
25+
def line(*rest)
26+
return stack(*rest)
27+
end
28+
29+
def loop(*rest)
30+
return ["("] + rest + [")+"]
31+
end
32+
33+
def toploop(*rest)
34+
return loop(*rest)
35+
end
36+
37+
def tailbranch(*rest)
38+
return rest
39+
end
40+
41+
def opt(*rest)
42+
return [rest, "?"] if rest.length <= 1
43+
return ["("] + rest + [")?"]
44+
end
45+
46+
def optx(*rest)
47+
return opt(*rest)
48+
end
49+
50+
# ----------------------------------------
51+
52+
print "// generated pegjs\n"
53+
print "\n"
54+
print "start = sql_stmt_list\n"
55+
print "\n"
56+
57+
grammar = ARGV[0]
58+
59+
load grammar
60+
61+
# ----------------------------------------
62+
63+
print """
64+
dot = '.'
65+
comma = ','
66+
semicolon = ';'
67+
minusminus = '--'
68+
minus = '-'
69+
plus = '+'
70+
lparen = '('
71+
rparen = ')'
72+
star = '*'
73+
newline = '\\n'
74+
anything_except_newline = [^\\n]*
75+
comment_beg = '/*'
76+
comment_end = '*/'
77+
anything_except_comment_end = .* & '*/'
78+
whitespace = [ \\t]*
79+
nil = ''
80+
"""
81+
82+
print "\n"
83+
84+
$keywords.keys.sort.each do |keyword|
85+
print "#{keyword} = whitespace \"#{keyword}\"\n"
86+
end
87+

0 commit comments

Comments
 (0)