From b9adad5f4179c8556f585dc0b9e9f31b665ba776 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Thu, 4 Apr 2024 18:35:34 +0900 Subject: Pass parsing spec/grammar/tests/tests.zig --- src/parse.janet | 125 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 62 insertions(+), 63 deletions(-) (limited to 'src') diff --git a/src/parse.janet b/src/parse.janet index 342fc11..9b08690 100644 --- a/src/parse.janet +++ b/src/parse.janet @@ -52,15 +52,11 @@ (peg/compile (merge (zipcoll (map |(keyword "keyword/" $) keywords) - (map |~(* (constant :keyword) - (<- ,$) - (! (+ :w "_")) - :skip) + (map |~(* ,$ (! (+ :w "_")) :skip) keywords)) (zipcoll (map keyword (keys ops)) - (map |~(* (<- ,(string $)) - (! (set ,(ops $))) - :skip) + (map |~(* (constant ,$) ,(string $) + (! (set ,(ops $))) :skip) (keys ops))) ~{:brace/l (* "{" :skip) :brace/r (* "}" :skip) @@ -88,7 +84,6 @@ :line-comment)) :doc-comment (* (constant :doc) (/ (some (* "///" - (* " ") (<- (to "\n")) :skip)) ,tuple)) @@ -120,33 +115,34 @@ :char/string (+ :char/escape (if (! (set "\n\"\\")) 1)) - :literal/integer (* (+ (* "0b" :int/bin) - (* "0o" :int/oct) - :int/dec - (* "0x" :int/hex)) - :skip) - :literal/float (* (+ (* :int/dec "." :int/dec (? :float-exp/dec)) - (* :int/dec :float-exp/dec) - (* "0x" :int/hex "." :int/hex (? :float-exp/hex)) - (* "0x" :int/hex :float-exp/hex)) + :literal/float (* (/ (<- (+ (* "0x" :int/hex "." :int/hex + (? :float-exp/hex)) + (* "0x" :int/hex :float-exp/hex) + (* :int/dec "." :int/dec + (? :float-exp/dec)) + (* :int/dec :float-exp/dec))) + ,|(->> $ + (string/replace "p" "&") + (string/replace "P" "&") + (scan-number))) :skip) - :literal/char (* "'" :char/char "'" :skip) + :literal/integer (* (+ (* "0b" (/ (<- :int/bin) ,|(scan-number $ 2))) + (* "0o" (/ (<- :int/oct) ,|(scan-number $ 8))) + (* "0x" (/ (<- :int/hex) ,|(scan-number $ 16))) + (/ (<- :int/dec) ,scan-number)) + :skip) + :literal/char (* "'" (/ (<- :char/char) ,|(symbol $)) "'" :skip) :literal/string/single (* "\"" (<- (any :char/string)) "\"" :skip) :literal/string (+ :literal/string/single (some (* (some (* "\\\\" (to "\n") (any (set " \n")))) :skip))) - :ident (+ (* (constant :ident) - (! :keyword) - (<- (* (+ :a "_") - (any (+ :w "_")))) + :ident (+ (* (! :keyword) + (<- (* (+ :a "_") (any (+ :w "_")))) :skip) - (* "@" :literal/string/single)) - :ident/builtin (* (constant :builtin) - "@" - (<- (* (+ :a "_") - (any (+ :w "_")))) + (* "@" (<- :literal/string/single))) + :ident/builtin (* "@" (<- (* (+ :a "_") (any (+ :w "_")))) :skip) :field-init (* :. :ident := :expr) :label/block (* :ident ::) @@ -226,22 +222,23 @@ :keyword/volatile :keyword/allowzero))) :proto/array (* :bracket/l :expr (? (* :: :expr)) :bracket/r) - :proto/type (+ :? (* :keyword/anyframe :->) + :proto/type (+ :? (* :keyword/anyframe :->) :proto/slice :proto/pointer :proto/array) :proto/decl-var (* (constant :const?) - (+ (* (constant true) (drop :keyword/const)) - (* (constant false) (drop :keyword/var))) - :ident + (+ (* (constant true) :keyword/const) + (* (constant false) :keyword/var)) + (constant :ident) :ident (? (* :: :expr/type)) (? :byte-align) (? :addr-space) (? :link-section)) - :proto/decl-var-rest (* :comma (+ :proto/decl-var :expr)) - :stmt/decl-var (* (+ (? (* :expr :op/assign)) - (* (+ :proto/decl-var - (* :expr :proto/decl-var-rest)) - (any :proto/decl-var-rest) - :=)) + :proto/decl-var/rest (* :comma (+ :proto/decl-var :expr)) + :stmt/decl-var (* (+ (* (+ :proto/decl-var + (* :expr :proto/decl-var/rest)) + (any :proto/decl-var/rest) + :=) + (* :expr :op/assign) + 0) :expr :semicolon) :stmt/for (* :proto/for (+ :expr/block @@ -253,25 +250,26 @@ (* :expr/assign :semicolon) (* (+ :expr/block :expr/assign) (* :keyword/else (? :payload) :stmt)))) - :stmt (+ (* :keyword/comptime - (+ :expr/block :stmt/decl-var)) - (* (+ :keyword/nosuspend - :keyword/suspend - :keyword/defer - (* :keyword/defer (? :payload))) - (+ :expr/block (* :expr/assign :semicolon))) - (* :proto/if - (+ :expr/block - (* :expr/assign :semicolon) - (* (+ :expr/block :expr/assign) - (* :keyword/else (? :payload) :stmt)))) - :expr/block - (* (? :label/block) - (? :keyword/inline) - (+ :stmt/for :stmt/while)) - :expr/switch - :stmt/decl-var) - :block (* :brace/l (any :stmt) :brace/r) + :stmt (/ (+ (* :keyword/comptime + (+ :expr/block :stmt/decl-var)) + (* (+ :keyword/nosuspend + :keyword/suspend + :keyword/defer + (* :keyword/defer (? :payload))) + (+ :expr/block (* :expr/assign :semicolon))) + (* :proto/if + (+ :expr/block + (* :expr/assign :semicolon) + (* (+ :expr/block :expr/assign) + (* :keyword/else (? :payload) :stmt)))) + :expr/block + (* (? :label/block) + (? :keyword/inline) + (+ :stmt/for :stmt/while)) + :expr/switch + :stmt/decl-var) + ,struct) + :block (* :brace/l (/ (any :stmt) ,tuple) :brace/r) :expr/block (* (? :label/block) :block) :expr/switch (* :keyword/switch :paren/l :expr :paren/r :brace/l (any (* :switch-prong :comma)) (? :switch-prong) @@ -290,9 +288,7 @@ (+ :proto/enum :expr) :paren/r)))) :brace/l :container/members :brace/r) - :expr/type (* (constant :type) - (/ (* (any :proto/type) :expr/error-union) - ,tuple)) + :expr/type (* (any :proto/type) :expr/error-union) :expr/type/primary (+ :literal/char :literal/float :literal/integer @@ -348,7 +344,8 @@ (* :proto/while :expr (? (* :keyword/else (? :payload) :expr))))) :block - (* :expr/type (? :list/init)))) + (* :expr/type :list/init) + :expr/type/primary)) :expr/mul (* :expr/prefix (any (* (+ ,(keyword "||") :** :* :*% ,(keyword "*|") @@ -374,9 +371,11 @@ (? (* :ident ::)) (+ :keyword/anytype :expr)) :...) - :decl/test (* (constant :kind) (constant :test) (drop :keyword/test) - (? (* (constant :name) (+ :literal/string/single :ident))) - :block) + :decl/test (* (constant :kind) (constant :test) :keyword/test + (+ (* (constant :name) :literal/string/single) + (* (constant :ident) :ident) + 0) + (constant :block) :block) :decl/comptime (* :keyword/comptime :block) :decl/fn (* (+ :keyword/export (* :keyword/extern (? :literal/string/single)) -- cgit 1.4.1