I really love (whenever possible) creating a set of grammar rules with positive terms to match the target input - I find it more literate, accurate, flexible and easier to debug. In the above snippet, we can identify five main components:
space: use [space][ space: charset "^-^/ " [some space] ] word: use [letter][ letter: charset [#"a" - #"z" #"A" - #"Z" "_"] [some letter] ] id: use [letter][ letter: complement charset "`" [some letter] ] number: use [digit][ digit: charset "0123456789" [some digit] ] string: use [char][ char: complement charset "'" [any [some char | "''"]] ]
With certain terms, writing a rule that describes the grammar of input is relatively trivial:
result: collect [ parsed?: parse/all data [ ; parse/all for Rebol 2 compatibility opt space some [ (field: type: none comment: copy "") "`" copy field id "`" space copy type word opt ["(" number ")"] any [ space [ "COMMENT" space "'" copy comment string "'" | word | "'" string "'" | number ] ] opt space "," (keep reduce [field type comment]) opt space ] ] ]
As an added bonus, we can confirm the entry.
if parsed? [new-line/all/skip result true 3]
One of the new-line applications, to make friends a little, should give:
== [ "id" "int" "" "name" "varchar" "the name" "content" "text" "something" ]
source share