Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flychecking fails with janet-uri library #1101

Open
meinside opened this issue Apr 14, 2023 · 1 comment
Open

flychecking fails with janet-uri library #1101

meinside opened this issue Apr 14, 2023 · 1 comment

Comments

@meinside
Copy link

Hi,

flychecking source files of projects which depend on janet-uri fails with following error:

error: /home/ubuntu/.asdf/installs/janet/1.27.0/lib/janet/uri.janet:96:33: compile error: (macro) grammar error in nil, unexpected peg source
  in peg/compile [src/core/peg.c] on line 1625
  in _thunk [eval] (tailcall) on line 96, column 43
  in dofile [boot.janet] (tailcall) on line 2868, column 7
  in source-loader [boot.janet] on line 2879, column 15
  in require-1 [boot.janet] on line 2899, column 18
  in import* [boot.janet] on line 2930, column 15
  in _thunk [src/XXXXXX.janet] (tailcall) on line 8, column 1
  in dofile [boot.janet] (tailcall) on line 2868, column 7

flycheck-evaluator seems to spit this error on this line.

I'm not sure if this issue is up to janet or janet-uri, so please correct me if I'm in the wrong neighborhood.

@sogaiu
Copy link
Contributor

sogaiu commented Apr 15, 2023

Based on some comments by bakpakin:

Fly check has a whitelist of top level forms that it will evaluate or specially process, forms like def and defn so that it knows which symbols are available. However, it won’t just blindly execute everything, so while you can put anything [...] at the top level scope in your module, fly check will likely be unable to see it.

I tried moving the two peg grammar definitions inside the functions they were used in [1] (each is only used in one place so this was doable).

That seemed to take care of things.

May be @andrewchambers has some thoughts about this issue.


[1] A diff looked like this:

diff --git a/uri.janet b/uri.janet
index 44eff00..81543c8 100644
--- a/uri.janet
+++ b/uri.janet
@@ -82,18 +82,20 @@
 #                  / "*" / "+" / "," / ";" / "="
 (import _uri :prefix "" :export true)
 
-(def- query-grammar ~{
-  :main (sequence (opt :query) (not 1))
-  :query (sequence :pair (any (sequence "&" :pair)))
-  :pair (sequence (cmt (capture :key) ,unescape) "=" (cmt (capture :value) ,unescape))
-  :key (any (sequence (not "=") 1))
-  :value (any (sequence (not "&") 1))
-})
-
 (defn parse-query
   [q]
   "Parse a uri encoded query string returning a table or nil."
-  (when-let [matches (peg/match (comptime (peg/compile query-grammar)) q)]
+  (def query-grammar
+    (comptime
+      (peg/compile
+        ~{:main (sequence (opt :query) (not 1))
+          :query (sequence :pair (any (sequence "&" :pair)))
+          :pair (sequence (cmt (capture :key) ,unescape)
+                          "="
+                          (cmt (capture :value) ,unescape))
+          :key (any (sequence (not "=") 1))
+          :value (any (sequence (not "&") 1))})))
+  (when-let [matches (peg/match query-grammar q)]
     (table ;matches)))
 
 (defn- named-capture
@@ -101,54 +103,6 @@
   (default name rule)
   ~(sequence (constant ,name) (capture ,rule)))
 
-(def- uri-grammar ~{
-  :main (sequence :URI-reference (not 1))
-  :URI-reference (choice :URI :relative-ref)
-  :URI (sequence ,(named-capture :scheme) ":" :hier-part (opt (sequence "?" ,(named-capture :query :raw-query)))  (opt (sequence "#" ,(named-capture :fragment :raw-fragment))))
-  :relative-ref (sequence :relative-part (opt (sequence "?" ,(named-capture :query :raw-query)))  (opt (sequence "#" ,(named-capture :fragment :raw-fragment))))
-  :hier-part (choice (sequence "//" :authority :path-abempty) :path-absolute :path-rootless :path-empty)
-  :relative-part (choice (sequence "//" :authority :path-abempty) :path-absolute :path-noscheme :path-empty)
-  :scheme (sequence :a (any (choice :a :d "+" "-" ".")))
-  :authority (sequence (opt (sequence ,(named-capture :userinfo) "@")) ,(named-capture :host) (opt (sequence ":" ,(named-capture :port))))
-  :userinfo (any (choice :unreserved :pct-encoded :sub-delims ":"))
-  :host (choice :IP-literal :IPv4address :reg-name)
-  :port (any :d)
-  :IP-literal (sequence "[" (choice :IPv6address :IPvFuture  ) "]" )
-  :IPv4address (sequence :dec-octet "." :dec-octet "." :dec-octet "." :dec-octet)
-  :IPvFuture (sequence "v" (at-least 1 :hexdig) "." (at-least 1 (sequence :unreserved :sub-delims ":" )))
-  :IPv6address (choice
-    (sequence (repeat 6 (sequence :h16 ":")) :ls32)
-    (sequence "::" (repeat 5 (sequence :h16 ":")) :ls32)
-    (sequence (opt :h16) "::" (repeat 4 (sequence :h16 ":")) :ls32)
-    (sequence (opt (sequence (at-most 1 (sequence :h16 ":")) :h16)) "::" (repeat 3 (sequence :h16 ":")) :ls32)
-    (sequence (opt (sequence (at-most 2 (sequence :h16 ":")) :h16)) "::" (repeat 2 (sequence :h16 ":")) :ls32)
-    (sequence (opt (sequence (at-most 3 (sequence :h16 ":")) :h16)) "::" (sequence :h16 ":") :ls32)
-    (sequence (opt (sequence (at-most 4 (sequence :h16 ":")) :h16)) "::" :ls32)
-    (sequence (opt (sequence (at-most 5 (sequence :h16 ":")) :h16)) "::" :h16)
-    (sequence (opt (sequence (at-most 6 (sequence :h16 ":")) :h16)) "::"))
-  :h16 (between 1 4 :hexdig)
-  :ls32 (choice (sequence :h16 ":" :h16) :IPv4address)
-  :dec-octet (choice (sequence "25" (range "05")) (sequence "2" (range "04") :d) (sequence "1" :d :d) (sequence (range "19") :d) :d)
-  :reg-name (any (choice :unreserved :pct-encoded :sub-delims))
-  :path (choice :path-abempty :path-absolute :path-noscheme :path-rootless :path-empty)
-  :path-abempty  ,(named-capture ~(any (sequence "/" :segment)) :raw-path)
-  :path-absolute ,(named-capture ~(sequence "/" (opt (sequence :segment-nz (any (sequence "/" :segment))))) :raw-path)
-  :path-noscheme ,(named-capture ~(sequence :segment-nz-nc (any (sequence "/" :segment))) :raw-path)
-  :path-rootless ,(named-capture ~(sequence :segment-nz (any (sequence "/" :segment))) :raw-path)
-  :path-empty (not :pchar)
-  :segment (any :pchar)
-  :segment-nz (some :pchar)
-  :segment-nz-nc (some (choice :unreserved :pct-encoded :sub-delims "@" ))
-  :pchar (choice :unreserved :pct-encoded :sub-delims ":" "@")
-  :query (any (choice :pchar (set "/?")))
-  :fragment (any (choice :pchar (set "/?")))
-  :pct-encoded (sequence "%" :hexdig :hexdig)
-  :unreserved (choice :a :d  (set "-._~"))
-  :gen-delims (set ":/?#[]@")
-  :sub-delims (set "!$&'()*+,;=")
-  :hexdig (choice :d (range "AF") (range "af"))
-})
-
 (defn parse-raw
   "Parse a uri-reference following rfc3986.
 
@@ -162,7 +116,99 @@
    returns nil if the input is not a valid uri.
   "
   [u &keys {:parse-query parse-query :unescape do-unescape}]
-  (when-let [matches (peg/match (comptime (peg/compile uri-grammar)) u)]
+  (def uri-grammar
+    (comptime
+      (peg/compile
+        ~{:main (sequence :URI-reference (not 1))
+          :URI-reference (choice :URI :relative-ref)
+          :URI
+          (sequence
+            ,(named-capture :scheme) ":" :hier-part
+            (opt (sequence "?" ,(named-capture :query :raw-query)))
+            (opt (sequence "#" ,(named-capture :fragment :raw-fragment))))
+          :relative-ref
+          (sequence
+            :relative-part
+            (opt (sequence "?" ,(named-capture :query :raw-query)))
+            (opt (sequence "#" ,(named-capture :fragment :raw-fragment))))
+          :hier-part (choice (sequence "//" :authority :path-abempty)
+                             :path-absolute :path-rootless :path-empty)
+          :relative-part (choice (sequence "//" :authority :path-abempty)
+                                 :path-absolute :path-noscheme :path-empty)
+          :scheme (sequence :a (any (choice :a :d "+" "-" ".")))
+          :authority (sequence (opt (sequence ,(named-capture :userinfo) "@"))
+                               ,(named-capture :host)
+                               (opt (sequence ":" ,(named-capture :port))))
+          :userinfo (any (choice :unreserved :pct-encoded :sub-delims ":"))
+          :host (choice :IP-literal :IPv4address :reg-name)
+          :port (any :d)
+          :IP-literal (sequence "[" (choice :IPv6address :IPvFuture  ) "]" )
+          :IPv4address (sequence :dec-octet "." :dec-octet "."
+                                 :dec-octet "." :dec-octet)
+          :IPvFuture
+          (sequence "v" (at-least 1 :hexdig) "."
+                    (at-least 1
+                              (sequence :unreserved :sub-delims ":" )))
+          :IPv6address
+          (choice
+            (sequence (repeat 6 (sequence :h16 ":")) :ls32)
+            (sequence "::" (repeat 5 (sequence :h16 ":")) :ls32)
+            (sequence (opt :h16) "::" (repeat 4 (sequence :h16 ":")) :ls32)
+            (sequence (opt (sequence (at-most 1 (sequence :h16 ":")) :h16))
+                      "::" (repeat 3 (sequence :h16 ":")) :ls32)
+            (sequence (opt (sequence (at-most 2 (sequence :h16 ":")) :h16))
+                      "::" (repeat 2 (sequence :h16 ":")) :ls32)
+            (sequence (opt (sequence (at-most 3 (sequence :h16 ":")) :h16))
+                      "::" (sequence :h16 ":") :ls32)
+            (sequence (opt (sequence (at-most 4 (sequence :h16 ":")) :h16))
+                      "::" :ls32)
+            (sequence (opt (sequence (at-most 5 (sequence :h16 ":")) :h16))
+                      "::" :h16)
+            (sequence (opt (sequence (at-most 6 (sequence :h16 ":")) :h16))
+                      "::"))
+          :h16 (between 1 4 :hexdig)
+          :ls32 (choice (sequence :h16 ":" :h16) :IPv4address)
+          :dec-octet (choice
+                       (sequence "25" (range "05"))
+                       (sequence "2" (range "04") :d)
+                       (sequence "1" :d :d)
+                       (sequence (range "19") :d) :d)
+          :reg-name (any (choice :unreserved :pct-encoded :sub-delims))
+          :path (choice :path-abempty :path-absolute
+                        :path-noscheme :path-rootless
+                        :path-empty)
+          :path-abempty  ,(named-capture
+                            ~(any (sequence "/" :segment))
+                            :raw-path)
+          :path-absolute ,(named-capture
+                            ~(sequence
+                               "/"
+                               (opt (sequence
+                                      :segment-nz
+                                      (any (sequence "/" :segment)))))
+                            :raw-path)
+          :path-noscheme ,(named-capture
+                            ~(sequence :segment-nz-nc
+                                       (any (sequence "/" :segment)))
+                            :raw-path)
+          :path-rootless ,(named-capture
+                            ~(sequence :segment-nz
+                                       (any (sequence "/" :segment)))
+                            :raw-path)
+          :path-empty (not :pchar)
+          :segment (any :pchar)
+          :segment-nz (some :pchar)
+          :segment-nz-nc (some (choice :unreserved :pct-encoded
+                                       :sub-delims "@" ))
+          :pchar (choice :unreserved :pct-encoded :sub-delims ":" "@")
+          :query (any (choice :pchar (set "/?")))
+          :fragment (any (choice :pchar (set "/?")))
+          :pct-encoded (sequence "%" :hexdig :hexdig)
+          :unreserved (choice :a :d  (set "-._~"))
+          :gen-delims (set ":/?#[]@")
+          :sub-delims (set "!$&'()*+,;=")
+          :hexdig (choice :d (range "AF") (range "af"))})))
+  (when-let [matches (peg/match uri-grammar u)]
     (table ;matches)))
 
 (defn parse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants