urQL/urql/lib/parse.hoon

312 lines
14 KiB
Plaintext
Raw Normal View History

2022-08-12 21:08:20 +03:00
/- ast
2022-08-21 03:29:26 +03:00
|_ current-database=@t :: (parse:parse(current-database '<db>') "<script>")
2022-08-12 21:08:20 +03:00
+$ command-ast
$%
2022-08-13 05:00:44 +03:00
create-database:ast
create-index:ast
create-namespace:ast
create-table:ast
create-view:ast
2022-08-16 22:18:14 +03:00
drop-database:ast
drop-index:ast
drop-namespace:ast
drop-table:ast
drop-view:ast
2022-08-17 20:10:50 +03:00
truncate-table:ast
2022-08-12 21:08:20 +03:00
==
+$ command
$%
%create-database
%create-index
%create-namespace
%create-table
%create-view
2022-08-16 22:18:14 +03:00
%drop-database
%drop-index
%drop-namespace
%drop-table
%drop-view
2022-08-17 20:10:50 +03:00
%truncate-table
2022-08-12 21:08:20 +03:00
==
2022-08-13 05:00:44 +03:00
::
2022-08-17 20:10:50 +03:00
:: parser rules and helpers
2022-08-16 22:18:14 +03:00
::
2022-08-17 20:10:50 +03:00
++ jester :: match a cord, case agnostic, thanks ~tinnus-napbus
2022-08-16 22:18:14 +03:00
|= daf=@t
|= tub=nail
=+ fad=daf
|- ^- (like @t)
?: =(`@`0 daf)
[p=p.tub q=[~ u=[p=fad q=tub]]]
2022-08-17 20:10:50 +03:00
=+ n=(end 3 daf)
?. ?& ?=(^ q.tub)
?| =(n i.q.tub)
&((lte 97 n) (gte 122 n) =((sub n 32) i.q.tub))
&((lte 65 n) (gte 90 n) =((add 32 n) i.q.tub))
==
==
2022-08-16 22:18:14 +03:00
(fail tub)
$(p.tub (lust i.q.tub p.tub), q.tub t.q.tub, daf (rsh 3 daf))
2022-08-21 03:29:26 +03:00
++ cook-qualified-object
|= a=*
?: ?=([@ [@ %~] [@ %~] [@ %~]] a) :: ~firsub.db.ns.name
(qualified-object:ast %qualified-object ``@p`-.a `@t`+<-.a `@t`+>-<.a `@t`+>+<.a)
?: ?=([@ [@ %~] * [@ %~]] a) ::~firsub..ns.name
(qualified-object:ast %qualified-object ``@p`-.a `@t`+<-.a 'dbo' `@t`+>+<.a)
?: ?=([[@ %~] [@ %~] [@ %~]] a) :: db.ns.name
(qualified-object:ast %qualified-object ~ `@t`-<.a `@t`+<-.a `@t`+>-.a)
?: ?=([[@ %~] * [@ %~]] a) :: db..name
(qualified-object:ast %qualified-object ~ `@t`-<.a 'dbo' `@t`+>-.a)
?: ?=([[@ %~] [@ %~]] a) :: ns.name
(qualified-object:ast %qualified-object ~ current-database `@t`-<.a `@t`+<.a)
?: ?=([@ %~] a) :: name
(qualified-object:ast %qualified-object ~ current-database 'dbo' `@t`-.a)
!!
::
:: parse urQL script
2022-08-13 05:00:44 +03:00
::
2022-08-06 17:06:18 +03:00
++ parse
2022-08-21 03:29:26 +03:00
|= script=tape
2022-08-06 17:06:18 +03:00
~| 'Input script is empty.'
?> !=((lent script) 0)
2022-08-13 05:13:25 +03:00
^- (list command-ast)
2022-08-13 05:00:44 +03:00
=/ commands `(list command-ast)`~
2022-08-13 05:13:25 +03:00
=/ script-position [1 1]
2022-08-12 21:08:20 +03:00
::
:: parser rules
::
=/ whitespace (star ;~(pose gah (just '\09') (just '\0d')))
2022-08-13 05:00:44 +03:00
=/ end-or-next-command ;~(pose ;~(plug whitespace mic) whitespace mic)
2022-08-12 21:08:20 +03:00
=/ parse-face ;~(pfix whitespace sym)
2022-08-16 22:18:14 +03:00
=/ parse-qualified-2-name ;~(pose ;~(pfix whitespace ;~((glue dot) sym sym)) parse-face)
=/ parse-qualified-3 ;~ pose
2022-08-17 20:10:50 +03:00
;~((glue dot) (star sym) (star sym) (star sym))
;~(plug (star sym) dot dot (star sym))
;~((glue dot) (star sym) (star sym))
(star sym)
==
2022-08-16 22:18:14 +03:00
=/ parse-qualified-3-name ;~(pfix whitespace parse-qualified-3)
=/ parse-force-or-3-name ;~(pose ;~(pfix whitespace (jester 'force')) parse-qualified-3-name)
2022-08-21 00:09:56 +03:00
=/ parse-ship ;~(pfix sig fed:ag)
2022-08-21 03:29:26 +03:00
=/ parse-qualified-object (cook cook-qualified-object ;~(pose ;~((glue dot) parse-ship (star sym) (star sym) (star sym)) ;~((glue dot) parse-ship (star sym) dot dot (star sym)) parse-qualified-3))
2022-08-17 20:10:50 +03:00
=/ parse-force-qualified-name ;~ sfix
;~(pose ;~(plug parse-force-or-3-name parse-qualified-3-name) parse-qualified-3-name)
end-or-next-command
==
2022-08-12 21:08:20 +03:00
=/ parse-command ;~ pose
2022-08-13 05:13:25 +03:00
(cold %create-database ;~(plug whitespace (jester 'create') whitespace (jester 'database')))
(cold %create-index ;~(plug whitespace (jester 'create') whitespace (jester 'index')))
(cold %create-namespace ;~(plug whitespace (jester 'create') whitespace (jester 'namespace')))
(cold %create-table ;~(plug whitespace (jester 'create') whitespace (jester 'table')))
(cold %create-view ;~(plug whitespace (jester 'create') whitespace (jester 'view')))
2022-08-16 22:18:14 +03:00
(cold %drop-database ;~(plug whitespace (jester 'drop') whitespace (jester 'database')))
(cold %drop-index ;~(plug whitespace (jester 'drop') whitespace (jester 'index')))
(cold %drop-namespace ;~(plug whitespace (jester 'drop') whitespace (jester 'namespace')))
(cold %drop-table ;~(plug whitespace (jester 'drop') whitespace (jester 'table')))
(cold %drop-view ;~(plug whitespace (jester 'drop') whitespace (jester 'view')))
2022-08-17 20:10:50 +03:00
(cold %truncate-table ;~(plug whitespace (jester 'truncate') whitespace (jester 'table')))
2022-08-13 05:13:25 +03:00
:: (cold ;~(plug whitespace (jester '') whitespace (jester '')))
2022-08-12 21:08:20 +03:00
==
2022-08-21 00:09:56 +03:00
~| 'Current database name is not a proper term'
2022-08-13 22:08:34 +03:00
=/ dummy (scan (trip current-database) sym)
2022-08-13 05:13:25 +03:00
:: main loop
2022-08-12 21:08:20 +03:00
::
2022-08-06 17:06:18 +03:00
|-
2022-08-12 21:08:20 +03:00
?: =(~ script) :: https://github.com/urbit/arvo/issues/1024
2022-08-13 05:00:44 +03:00
(flop commands)
~| "Error parsing command keyword: {<script-position>}"
=/ command-nail u.+3:q.+3:(parse-command [script-position script])
2022-08-12 21:08:20 +03:00
?- `command`p.command-nail
%create-database
2022-08-14 00:20:30 +03:00
~| 'Create database must be only statement in script'
?> =((lent commands) 0)
%= $
2022-08-16 22:18:14 +03:00
script ""
commands
[`command-ast`(create-database:ast %create-database p.u.+3:q.+3:(parse-face [[1 1] q.q.command-nail])) commands]
2022-08-14 00:20:30 +03:00
==
2022-08-12 21:08:20 +03:00
%create-index
!!
%create-namespace
2022-08-17 19:26:46 +03:00
=/ parse-create-namespace ;~ sfix
parse-qualified-2-name
end-or-next-command
==
~| "Cannot parse name to face in create-namespace {<p.q.command-nail>}"
=/ create-namespace-nail (parse-create-namespace [[1 1] q.q.command-nail])
2022-08-17 20:10:50 +03:00
=/ parsed (wonk create-namespace-nail)
2022-08-17 19:26:46 +03:00
=/ cursor p.q.u.+3:q.+3:create-namespace-nail
=/ next-cursor ?: (gth -.cursor -.script-position) :: if we advanced to next input line
[(add -.cursor -.script-position) +.cursor] :: add lines and use nail cursor column
[-.cursor (add +.cursor +.script-position)] :: else add column positions
?@ parsed
%= $
script q.q.u.+3.q:create-namespace-nail
script-position next-cursor
commands [`command-ast`(create-namespace:ast %create-namespace current-database parsed) commands]
2022-08-13 05:00:44 +03:00
==
%= $
2022-08-17 19:26:46 +03:00
script q.q.u.+3.q:create-namespace-nail
script-position next-cursor
commands [`command-ast`(create-namespace:ast %create-namespace -.parsed +.parsed) commands]
2022-08-13 05:00:44 +03:00
==
2022-08-12 21:08:20 +03:00
%create-table
!!
%create-view
!!
2022-08-16 22:18:14 +03:00
%drop-database
!!
%drop-index
!!
%drop-namespace
!!
%drop-table
2022-08-17 19:35:12 +03:00
~| "Cannot parse drop-table {<p.q.command-nail>}"
2022-08-17 20:10:50 +03:00
=/ drop-table-nail (parse-force-qualified-name [[1 1] q.q.command-nail])
=/ parsed (wonk drop-table-nail)
2022-08-17 19:35:12 +03:00
=/ cursor p.q.u.+3:q.+3:drop-table-nail
=/ next-cursor ?: (gth -.cursor -.script-position) :: if we advanced to next input line
[(add -.cursor -.script-position) +.cursor] :: add lines and use nail cursor column
[-.cursor (add +.cursor +.script-position)] :: else add column positions
2022-08-17 20:10:50 +03:00
?: ?=([@ [[@ %~] [@ %~] [@ %~]]] parsed) :: "drop table force db.ns.name"
2022-08-17 19:35:12 +03:00
%= $
script q.q.u.+3.q:drop-table-nail
script-position next-cursor
commands
[`command-ast`(drop-table:ast %drop-table i.+<.parsed i.+>-.parsed i.+>+.parsed %.y) commands]
==
2022-08-17 20:10:50 +03:00
?: ?=([@ [[@ %~] @ [@ %~]]] parsed) :: "drop table force db..name"
2022-08-17 19:35:12 +03:00
%= $
script q.q.u.+3.q:drop-table-nail
script-position next-cursor
commands
2022-08-17 20:10:50 +03:00
[`command-ast`(drop-table:ast %drop-table i.+<.parsed 'dbo' +>+<.parsed %.y) commands]
2022-08-17 19:35:12 +03:00
==
2022-08-17 20:10:50 +03:00
?: ?=([@ [[@ %~] [@ %~]]] parsed) :: "drop table force ns.name"
2022-08-17 19:35:12 +03:00
%= $
script q.q.u.+3.q:drop-table-nail
script-position next-cursor
commands
[`command-ast`(drop-table:ast %drop-table current-database i.+<.parsed +>-.parsed %.y) commands]
==
2022-08-17 20:10:50 +03:00
?: ?=([@ [@ %~]] parsed) :: "drop table force name"
2022-08-17 19:35:12 +03:00
%= $
script q.q.u.+3.q:drop-table-nail
script-position next-cursor
commands
[`command-ast`(drop-table:ast %drop-table current-database 'dbo' +<.parsed %.y) commands]
==
2022-08-17 20:10:50 +03:00
?: ?=([[[@ %~] [@ %~] [@ %~]] %~] parsed) :: "drop table db.ns.name"
2022-08-17 19:35:12 +03:00
%= $
script q.q.u.+3.q:drop-table-nail
script-position next-cursor
commands
[`command-ast`(drop-table:ast %drop-table i.-<.parsed i.->-.parsed i.->+.parsed %.n) commands]
==
2022-08-17 20:10:50 +03:00
?: ?=([[[@ %~] @ [@ %~]] %~] parsed) :: "drop table db..name"
2022-08-17 19:35:12 +03:00
%= $
script q.q.u.+3.q:drop-table-nail
script-position next-cursor
commands
2022-08-17 20:10:50 +03:00
[`command-ast`(drop-table:ast %drop-table i.-<.parsed 'dbo' ->+<.parsed %.n) commands]
2022-08-17 19:35:12 +03:00
==
2022-08-17 20:10:50 +03:00
?: ?=([[[@ %~] [@ %~]] %~] parsed) :: "drop table ns.name"
2022-08-17 19:35:12 +03:00
%= $
script q.q.u.+3.q:drop-table-nail
script-position next-cursor
commands
[`command-ast`(drop-table:ast %drop-table current-database i.-<.parsed ->-.parsed %.n) commands]
==
2022-08-17 20:10:50 +03:00
?: ?=([[@ %~] %~] parsed) :: "drop table name"
2022-08-17 19:35:12 +03:00
%= $
script q.q.u.+3.q:drop-table-nail
script-position next-cursor
commands
[`command-ast`(drop-table:ast %drop-table current-database 'dbo' -<.parsed %.n) commands]
==
2022-08-16 22:18:14 +03:00
!!
%drop-view
~| "Cannot parse drop-view {<p.q.command-nail>}"
2022-08-17 20:10:50 +03:00
=/ drop-view-nail (parse-force-qualified-name [[1 1] q.q.command-nail])
=/ parsed (wonk drop-view-nail)
2022-08-16 22:18:14 +03:00
=/ cursor p.q.u.+3:q.+3:drop-view-nail
=/ next-cursor ?: (gth -.cursor -.script-position) :: if we advanced to next input line
2022-08-17 19:26:46 +03:00
[(add -.cursor -.script-position) +.cursor] :: add lines and use nail cursor column
[-.cursor (add +.cursor +.script-position)] :: else add column positions
2022-08-17 20:10:50 +03:00
?: ?=([@ [[@ %~] [@ %~] [@ %~]]] parsed) :: "drop view force db.ns.name"
2022-08-16 22:18:14 +03:00
%= $
script q.q.u.+3.q:drop-view-nail
script-position next-cursor
commands
[`command-ast`(drop-view:ast %drop-view i.+<.parsed i.+>-.parsed i.+>+.parsed %.y) commands]
==
2022-08-17 20:10:50 +03:00
?: ?=([@ [[@ %~] @ [@ %~]]] parsed) :: "drop view force db..name"
2022-08-16 22:18:14 +03:00
%= $
script q.q.u.+3.q:drop-view-nail
script-position next-cursor
commands
2022-08-17 20:10:50 +03:00
[`command-ast`(drop-view:ast %drop-view i.+<.parsed 'dbo' +>+<.parsed %.y) commands]
2022-08-16 22:18:14 +03:00
==
2022-08-17 20:10:50 +03:00
?: ?=([@ [[@ %~] [@ %~]]] parsed) :: "drop view force ns.name"
2022-08-16 22:18:14 +03:00
%= $
script q.q.u.+3.q:drop-view-nail
script-position next-cursor
commands
[`command-ast`(drop-view:ast %drop-view current-database i.+<.parsed +>-.parsed %.y) commands]
==
2022-08-17 20:10:50 +03:00
?: ?=([@ [@ %~]] parsed) :: "drop view force name"
2022-08-16 22:18:14 +03:00
%= $
script q.q.u.+3.q:drop-view-nail
script-position next-cursor
commands
[`command-ast`(drop-view:ast %drop-view current-database 'dbo' +<.parsed %.y) commands]
==
2022-08-17 20:10:50 +03:00
?: ?=([[[@ %~] [@ %~] [@ %~]] %~] parsed) :: "drop view db.ns.name"
2022-08-16 22:18:14 +03:00
%= $
script q.q.u.+3.q:drop-view-nail
script-position next-cursor
commands
[`command-ast`(drop-view:ast %drop-view i.-<.parsed i.->-.parsed i.->+.parsed %.n) commands]
==
2022-08-17 20:10:50 +03:00
?: ?=([[[@ %~] @ [@ %~]] %~] parsed) :: "drop view db..name"
2022-08-16 22:18:14 +03:00
%= $
script q.q.u.+3.q:drop-view-nail
script-position next-cursor
commands
2022-08-17 20:10:50 +03:00
[`command-ast`(drop-view:ast %drop-view i.-<.parsed 'dbo' ->+<.parsed %.n) commands]
2022-08-16 22:18:14 +03:00
==
2022-08-17 20:10:50 +03:00
?: ?=([[[@ %~] [@ %~]] %~] parsed) :: "drop view ns.name"
2022-08-16 22:18:14 +03:00
%= $
script q.q.u.+3.q:drop-view-nail
script-position next-cursor
commands
[`command-ast`(drop-view:ast %drop-view current-database i.-<.parsed ->-.parsed %.n) commands]
==
2022-08-17 20:10:50 +03:00
?: ?=([[@ %~] %~] parsed) :: "drop view name"
2022-08-16 22:18:14 +03:00
%= $
script q.q.u.+3.q:drop-view-nail
script-position next-cursor
commands
[`command-ast`(drop-view:ast %drop-view current-database 'dbo' -<.parsed %.n) commands]
==
!!
2022-08-17 20:10:50 +03:00
%truncate-table
=/ parse-truncate-table ;~ sfix
2022-08-21 00:09:56 +03:00
;~(pfix whitespace parse-qualified-object)
2022-08-17 20:10:50 +03:00
end-or-next-command
==
~| "Cannot parse truncate-table {<p.q.command-nail>}"
=/ truncate-table-nail (parse-truncate-table [[1 1] q.q.command-nail])
=/ cursor p.-.truncate-table-nail
=/ next-cursor ?: (gth -.cursor -.script-position) :: if we advanced to next input line
[(add -.cursor -.script-position) +.cursor] :: add lines and use nail cursor column
[-.cursor (add +.cursor +.script-position)] :: else add column positions
2022-08-21 03:29:26 +03:00
%= $
script q.q.u.+3.q:truncate-table-nail
script-position next-cursor
commands
[`command-ast`(truncate-table:ast %truncate-table (wonk truncate-table-nail)) commands]
==
2022-08-16 22:18:14 +03:00
==
2022-08-13 05:13:25 +03:00
--