Last active
December 6, 2020 19:41
-
-
Save rgchris/f9f070c15afb64022d1361f191baa16c to your computer and use it in GitHub Desktop.
SQL Lexer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rebol [ | |
| Title: "SQL Lexer" | |
| Date: 24-Jul-2020 | |
| Author: "Christopher Ross-Gill" | |
| ] | |
| lexer: make object! [ | |
| whitespace: charset " ^-" | |
| delimiters: charset {"'(),.;`} | |
| chars: complement union union whitespace delimiters charset "^/^M" | |
| digit: charset "0123456789" | |
| lower-alpha: charset [#"a" - #"z"] | |
| upper-alpha: charset [#"a" - #"z"] | |
| alpha: union lower-alpha upper-alpha | |
| word-literal: complement charset "`" | |
| comment-line: complement charset "^/^M" | |
| comment: complement charset "*" | |
| quoted-single: complement charset {'\} | |
| quoted-double: complement charset {"\} | |
| type: _ | |
| part: _ | |
| delimiter: ";" | |
| value: [ | |
| "--" any comment-line | |
| (type: 'comment-line) | |
| | | |
| "/*" any [comment | "*" not "/"] "*/" | |
| (type: 'comment) | |
| | | |
| #"'" any ["\\" | "\'" | "''" | quoted-single] #"'" | |
| (type: 'string-single) | |
| | | |
| #"^"" any ["\\" | {\"} | {""} | quoted-double] #"^"" | |
| (type: 'string-double) | |
| | | |
| #"`" some word-literal #"`" | |
| (type: 'literal) | |
| | | |
| #"@" opt #"@" some [some lower-alpha | #"." | #"_"] ; include "$" here? | |
| (type: 'variable) | |
| | | |
| alpha any [alpha | digit | #"_"] ; include #"$" here? | |
| (type: 'word) | |
| | | |
| opt #"-" some digit opt ["." some digit] opt [[#"e" | #"E"] opt #"-" some digit] | |
| (type: 'number) | |
| | | |
| some chars ; misc | |
| (type: 'other) | |
| ] | |
| statement: [ | |
| any [ | |
| #"^/" | change [#"^M" opt #"^/"] #"^/" | |
| | | |
| change copy part some whitespace (detab part) | |
| | | |
| delimiter | |
| | | |
| #"(" | #")" | |
| | | |
| #"," | #"." | |
| | | |
| change copy part value (rejoin ["«" form type "»«" part "»"]) | |
| | | |
| end | |
| | | |
| (info "SKIP: Should Not Happen") ?? skip | |
| ] | |
| ] | |
| ] | |
| parse-sql: func [ | |
| statement [text!] | |
| <local> is-sql | |
| ][ | |
| assert [ | |
| all [ | |
| did is-sql: parse copy statement lexer/statement | |
| tail? is-sql | |
| ] | |
| ] | |
| head is-sql | |
| ] | |
| probe parse-sql {Select * From `something`} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment