Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bubble-to-pegjs.rb first attempt

  • Loading branch information...
commit 386559200ed36e2a713df923f9532b93b16b4cfc 1 parent cf35d7f
@steveyen authored
Showing with 91 additions and 0 deletions.
  1. +1 −0  .gitignore
  2. +3 −0  Makefile
  3. +87 −0 bubble-to-pegjs.rb
View
1  .gitignore
@@ -0,0 +1 @@
+tmp
View
3  Makefile
@@ -0,0 +1,3 @@
+all:
+ ./sql-bubble.rb > tmp/rules.rb
+ ./bubble-to-pegjs.rb tmp/rules.rb > tmp/sql.pegjs
View
87 bubble-to-pegjs.rb
@@ -0,0 +1,87 @@
+#!/usr/bin/env ruby
+
+$keywords = {}
+
+def rule(name, *rest)
+ print "#{name} =\n"
+ rest.flatten.each do |x|
+ $keywords[x] = true if x.class == String and x.match(/^[A-Z]+$/)
+ print "#{x} "
+ end
+ print "\n\n"
+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)
+ return rest
+end
+
+def opt(*rest)
+ return [rest, "?"] if rest.length <= 1
+ return ["("] + rest + [")?"]
+end
+
+def optx(*rest)
+ return opt(*rest)
+end
+
+# ----------------------------------------
+
+print "// generated pegjs\n"
+print "\n"
+print "start = sql_stmt_list\n"
+print "\n"
+
+grammar = ARGV[0]
+
+load grammar
+
+# ----------------------------------------
+
+print """
+dot = '.'
+comma = ','
+semicolon = ';'
+minusminus = '--'
+minus = '-'
+plus = '+'
+lparen = '('
+rparen = ')'
+star = '*'
+newline = '\\n'
+anything_except_newline = [^\\n]*
+comment_beg = '/*'
+comment_end = '*/'
+anything_except_comment_end = .* & '*/'
+whitespace = [ \\t]*
+nil = ''
+"""
+
+print "\n"
+
+$keywords.keys.sort.each do |keyword|
+ print "#{keyword} = whitespace \"#{keyword}\"\n"
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.