Skip to content

Instantly share code, notes, and snippets.

@rgchris
Last active December 6, 2020 19:41
Show Gist options
  • Select an option

  • Save rgchris/f9f070c15afb64022d1361f191baa16c to your computer and use it in GitHub Desktop.

Select an option

Save rgchris/f9f070c15afb64022d1361f191baa16c to your computer and use it in GitHub Desktop.
SQL Lexer
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: 'word)
|
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