Paste: parse sql

Author: Jim Mack
Mode: factor
Date: Mon, 15 Feb 2010 02:07:22
Plain Text |
USING: prettyprint  
nested-comments formatting   
accessors arrays assocs calendar combinators fry kernel
generalizations io io.streams.string macros math math.functions
math.parser peg.ebnf quotations sequences splitting strings
unicode.categories unicode.case vectors combinators.smart
peg
;
IN: scratchpad


TUPLE: section name ;
: <section> ( str -- section ) >lower section boa ;
TUPLE: punctuation name ;
: <punctuation> ( str -- punctuation )  punctuation boa ;
TUPLE: qualified table column ;
: <qualified> ( table col -- qual ) qualified boa ;
TUPLE: comparison name ;
: <comparison> ( str -- comparison )  comparison boa ;
EBNF: sql-parts

select = ("select"|"SELECT"|"Select") => [[ <section> ]]
from = ("from"|"FROM"|"From") => [[ <section> ]]
where = ("where"|"WHERE"|"Where") => [[ <section> ]]
order = ("order by"|"ORDER BY"|"Order By") => [[ <section> ]]
group = ("group by"|"GROUP BY"|"Group By") => [[ <section> ]]
slash = ("/") => [[ <section> ]]
section = (select|from|where|order|group|slash)

period = "."
comma = ","
comparing = ("=",">","<")
full-column = plain-text:a period plain-text:b => [[ a b <qualified> ]]
plain-text = (!("%" | " " | "/" | "." | "," ).)+          => [[ >string  ]]
puncs = (period|comma) => [[ <punctuation>  ]]
sp = (" " | "\r" | "\n" | "\t" )+ => [[ drop f ]]

text      = (section|plain-text|sp|full-column|puncs) 
texts = text* => [[ sift ]]

;EBNF  


EBNF: sql-phrases
tokenizer = <foreign sql-parts text>
select-part = . ?[ section? ]? => [[ name>> ]]
text = . ?[ string? ]? => [[ ]]
rule = select-part:a text:b => [[ a b 2array ]]
;EBNF

CONSTANT: example-sql " select top 3 acctid as AccountID, acctnum AccountNum, blah 'blah  blah blah' from grampa g join son on g.id = son.grampa where g.id < 3 and s.active = 1" 
example-sql sql-parts . 
! example-sql sql-phrases .

(*  The steps I'm tripping on:  
    1) sql-phrases doesn't work
    2) don't get how to make a rule to keep 'blah  blah blah' together
    3) some guidance how to group things:  I don't understand why full-column rule doesn't grab g.id, for example
*)

New Annotation

Summary:
Author:
Mode:
Body: