From f2048235f5cc7ff02db39a0e2fe5c79c7f390e0b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 21 Sep 2018 00:30:28 -0700 Subject: Incremental checkin, currently not working, just saving progress. --- nomnom/parser.nom | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 nomnom/parser.nom (limited to 'nomnom/parser.nom') diff --git a/nomnom/parser.nom b/nomnom/parser.nom new file mode 100644 index 0000000..60c356f --- /dev/null +++ b/nomnom/parser.nom @@ -0,0 +1,81 @@ +# This file contains the parser, which converts text into abstract syntax trees +use "nomonom/ast.nom" + +%lpeg = (=lua "require('lpeg')") +%re = (=lua "require('re')") +call %lpeg.setmaxstack with [20_000] +set {..} + (action (P 1)): %lpeg.P, (action (R 1)): %lpeg.R, (action (Carg 1)): %lpeg.Carg, + (action (Cc 1)): %lpeg.Cc, (action (lpeg re pattern 1)): %re.compile, + (action (lpeg re pattern 1 using 2)): %re.compile + +%source_code_for_tree = {} +%defs = (..) + {..} + nl: (P "\r")^(-1) * (P "\n") + tab: P "\t" + tonumber: %tonumber + tochar: %string.char + unpack: %unpack + nil: Cc (nil) + userdata: Carg 1 + utf8_char: (..) + (R "\194\223")*(R "\128\191") + + (R "\224\239")*(R "\128\191")*(R "\128\191") + + (R "\240\244")*(R "\128\191")*(R "\128\191")*(R "\128\191") + + Tree: [%t, %userdata] ->: + %source = (..) + Source {filename:%userdata.filename, start:%tree.start, stop:%tree.stop} + set {%t.start: nil, %t.stop: nil} + %t = (Syntax Tree %t) + (Syntax Tree).source_code_for_tree.%t = %userdata.source + return %t + + ..with fallback %key ->: + if: + (%key::matches "^ascii_(%d+)$"): + %i = (%key::matching "^ascii_(%d+)$") + return (call %string.char with [%i as a number]) + (%key::matches "^number_(%d+)$"): + %i = (%key::matching "^number_(%d+)$") + return (Cc (%i as a number)) + +%id_patt = (((P "") - (R "09")) * ((%defs.utf8_char + (R "az") + (R "AZ") + (P "_") + (R "09"))^1 * -1)) +%operator_patt = ((S "'`~!@$^&*+=|<>?/-")^1 * -1) +%text_methods = (""'s metatable).__index +%text_methods.(action (is a nomsu identifier)) = (..) + [%str] -> (call %id_patt.match with [%id_patt, %str]) +%text_methods.(action (is a nomsu id)) = %text_methods.(action (is a nomsu identifier)) +%text_methods.(action (is a nomsu operator)) = (..) + [%str] -> (call %operator_patt.match with [%operator_patt, %str]) + +%peg_tidier = (..) + lpeg re pattern "\ + file <- %nl* {~ (def/comment) (%nl+ (def/comment))* %nl* ~} + def <- anon_def / captured_def + anon_def <- + ({ident} (" "*) ":" {[^%nl]* (%nl+ " "+ [^%nl]*)*}) + -> "%1 <- %2" + captured_def <- + ({ident} (" "*) "(" {ident} ")" (" "*) ":" {[^%nl]* (%nl+ " "+ [^%nl]*)*}) + -> "%1 <- ({| {:start:{}:} %3 {:stop:{}:} {:type: (''->'%2') :} |} %%userdata) -> Tree" + ident <- [a-zA-Z_][a-zA-Z0-9_]* + comment <- "--" [^%nl]* + " + +action [make parser from %peg] (make parser from %peg using (nil)) + +action [make parser from %peg using %make_tree]: + %peg = (call %peg_tidier.match with [%peg_tidier, %peg]) + %peg = (lpeg re pattern %peg using %defs) + local action [parse %input from %filename]: + %input = "\%input" + %tree_mt = {__index: {source:%input, filename:%filename}} + %userdata = {..} + make_tree: %make_tree or ([%]-> (: set %'s metatable to %tree_mt; return %)) + filename:%filename, source:%input + %tree = (call %peg.match with [%peg, %input, (nil), %userdata]) + assume %tree or barf "File \%filename failed to parse:\n\%input" + return %tree + return (action (parse 1 from 2)) -- cgit v1.2.3 From b43432e647fbb3bb76aa2836e3899d5e407c50f9 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 13:05:28 -0700 Subject: Fixed all syntax errors, got original (non-nomnom) tests passing. --- nomnom/parser.nom | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nomnom/parser.nom') diff --git a/nomnom/parser.nom b/nomnom/parser.nom index 60c356f..1f460bd 100644 --- a/nomnom/parser.nom +++ b/nomnom/parser.nom @@ -21,8 +21,8 @@ set {..} userdata: Carg 1 utf8_char: (..) (R "\194\223")*(R "\128\191") + - (R "\224\239")*(R "\128\191")*(R "\128\191") + - (R "\240\244")*(R "\128\191")*(R "\128\191")*(R "\128\191") + ..(R "\224\239")*(R "\128\191")*(R "\128\191") + + ..(R "\240\244")*(R "\128\191")*(R "\128\191")*(R "\128\191") Tree: [%t, %userdata] ->: %source = (..) -- cgit v1.2.3 From 3f31b09e7404e1ea374bbeb230bf34664b641efb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 14:00:05 -0700 Subject: Updated to the point of actually compiling. --- nomnom/parser.nom | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'nomnom/parser.nom') diff --git a/nomnom/parser.nom b/nomnom/parser.nom index 1f460bd..dc98c24 100644 --- a/nomnom/parser.nom +++ b/nomnom/parser.nom @@ -1,11 +1,12 @@ # This file contains the parser, which converts text into abstract syntax trees -use "nomonom/ast.nom" +#use "nomonom/ast.nom" %lpeg = (=lua "require('lpeg')") %re = (=lua "require('re')") -call %lpeg.setmaxstack with [20_000] +call %lpeg.setmaxstack with [20000] set {..} (action (P 1)): %lpeg.P, (action (R 1)): %lpeg.R, (action (Carg 1)): %lpeg.Carg, + (action (S 1)): %lpeg.S, (action (Cc 1)): %lpeg.Cc, (action (lpeg re pattern 1)): %re.compile, (action (lpeg re pattern 1 using 2)): %re.compile @@ -43,11 +44,11 @@ set {..} %id_patt = (((P "") - (R "09")) * ((%defs.utf8_char + (R "az") + (R "AZ") + (P "_") + (R "09"))^1 * -1)) %operator_patt = ((S "'`~!@$^&*+=|<>?/-")^1 * -1) -%text_methods = (""'s metatable).__index -%text_methods.(action (is a nomsu identifier)) = (..) +%text_methods = (""'s metatable).__methods +%text_methods.("is a nomsu identifier"::as lua id) = (..) [%str] -> (call %id_patt.match with [%id_patt, %str]) -%text_methods.(action (is a nomsu id)) = %text_methods.(action (is a nomsu identifier)) -%text_methods.(action (is a nomsu operator)) = (..) +%text_methods.("is a nomsu id"::as lua id) = %text_methods.("is a nomsu identifier"::as lua id) +%text_methods.("is a nomsu operator"::as lua id) = (..) [%str] -> (call %operator_patt.match with [%operator_patt, %str]) %peg_tidier = (..) -- cgit v1.2.3 From 678344182b1f04e35063d7185ac1d74317b011ea Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 28 Sep 2018 18:36:36 -0700 Subject: Forward progress on getting nomnom working. --- nomnom/parser.nom | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'nomnom/parser.nom') diff --git a/nomnom/parser.nom b/nomnom/parser.nom index dc98c24..ef06e08 100644 --- a/nomnom/parser.nom +++ b/nomnom/parser.nom @@ -9,6 +9,8 @@ set {..} (action (S 1)): %lpeg.S, (action (Cc 1)): %lpeg.Cc, (action (lpeg re pattern 1)): %re.compile, (action (lpeg re pattern 1 using 2)): %re.compile + (action (lpeg pattern 1's match of 2)): %lpeg.match + (action (lpeg pattern 1's match of 2 with 3)): [%1, %2, %3] -> (call %lpeg.match with [%1, %2, nil, %3]) %source_code_for_tree = {} %defs = (..) @@ -27,7 +29,7 @@ set {..} Tree: [%t, %userdata] ->: %source = (..) - Source {filename:%userdata.filename, start:%tree.start, stop:%tree.stop} + Source {filename:%userdata.filename, start:%t.start, stop:%t.stop} set {%t.start: nil, %t.stop: nil} %t = (Syntax Tree %t) (Syntax Tree).source_code_for_tree.%t = %userdata.source @@ -44,12 +46,12 @@ set {..} %id_patt = (((P "") - (R "09")) * ((%defs.utf8_char + (R "az") + (R "AZ") + (P "_") + (R "09"))^1 * -1)) %operator_patt = ((S "'`~!@$^&*+=|<>?/-")^1 * -1) -%text_methods = (""'s metatable).__methods -%text_methods.("is a nomsu identifier"::as lua id) = (..) - [%str] -> (call %id_patt.match with [%id_patt, %str]) -%text_methods.("is a nomsu id"::as lua id) = %text_methods.("is a nomsu identifier"::as lua id) -%text_methods.("is a nomsu operator"::as lua id) = (..) - [%str] -> (call %operator_patt.match with [%operator_patt, %str]) + +action [%text is a nomsu id, %text is a nomsu identifier] (..) + lpeg pattern %id_patt's match of %text + +action [%text is a nomsu operator] (..) + lpeg pattern %operator_patt's match of %text %peg_tidier = (..) lpeg re pattern "\ @@ -68,7 +70,7 @@ set {..} action [make parser from %peg] (make parser from %peg using (nil)) action [make parser from %peg using %make_tree]: - %peg = (call %peg_tidier.match with [%peg_tidier, %peg]) + %peg = (lpeg pattern %peg_tidier's match of %peg) %peg = (lpeg re pattern %peg using %defs) local action [parse %input from %filename]: %input = "\%input" @@ -76,7 +78,7 @@ action [make parser from %peg using %make_tree]: %userdata = {..} make_tree: %make_tree or ([%]-> (: set %'s metatable to %tree_mt; return %)) filename:%filename, source:%input - %tree = (call %peg.match with [%peg, %input, (nil), %userdata]) + %tree = (lpeg pattern %peg's match of %input with %userdata) assume %tree or barf "File \%filename failed to parse:\n\%input" return %tree return (action (parse 1 from 2)) -- cgit v1.2.3 From 63d8b1cd3f34b15bf86210b99209e8b57e7019bb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 28 Sep 2018 22:15:06 -0700 Subject: Fully working, I think? (with a lot of shims) --- nomnom/parser.nom | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nomnom/parser.nom') diff --git a/nomnom/parser.nom b/nomnom/parser.nom index ef06e08..0be394c 100644 --- a/nomnom/parser.nom +++ b/nomnom/parser.nom @@ -30,7 +30,7 @@ set {..} Tree: [%t, %userdata] ->: %source = (..) Source {filename:%userdata.filename, start:%t.start, stop:%t.stop} - set {%t.start: nil, %t.stop: nil} + set {%t.start: nil, %t.stop: nil, %t.source: %source} %t = (Syntax Tree %t) (Syntax Tree).source_code_for_tree.%t = %userdata.source return %t -- cgit v1.2.3 From 2f68357cb6800e97edd31abfc707d7c7905faa64 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 3 Oct 2018 16:26:24 -0700 Subject: Some incremental progress. --- nomnom/parser.nom | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nomnom/parser.nom') diff --git a/nomnom/parser.nom b/nomnom/parser.nom index 0be394c..dfd5253 100644 --- a/nomnom/parser.nom +++ b/nomnom/parser.nom @@ -72,7 +72,7 @@ action [make parser from %peg] (make parser from %peg using (nil)) action [make parser from %peg using %make_tree]: %peg = (lpeg pattern %peg_tidier's match of %peg) %peg = (lpeg re pattern %peg using %defs) - local action [parse %input from %filename]: + local action [%input from %filename parsed]: %input = "\%input" %tree_mt = {__index: {source:%input, filename:%filename}} %userdata = {..} @@ -81,4 +81,4 @@ action [make parser from %peg using %make_tree]: %tree = (lpeg pattern %peg's match of %input with %userdata) assume %tree or barf "File \%filename failed to parse:\n\%input" return %tree - return (action (parse 1 from 2)) + return (action (1 from 2 parsed)) -- cgit v1.2.3 From 7a35e38d8778670fe0662f203e82638355db3bba Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 31 Oct 2018 15:05:17 -0700 Subject: Renamed (action %) -> (%'s meaning) --- nomnom/parser.nom | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'nomnom/parser.nom') diff --git a/nomnom/parser.nom b/nomnom/parser.nom index dfd5253..13d6112 100644 --- a/nomnom/parser.nom +++ b/nomnom/parser.nom @@ -1,16 +1,17 @@ +#!/usr/bin/env nomsu -V4.8.10 # This file contains the parser, which converts text into abstract syntax trees #use "nomonom/ast.nom" - %lpeg = (=lua "require('lpeg')") %re = (=lua "require('re')") call %lpeg.setmaxstack with [20000] set {..} - (action (P 1)): %lpeg.P, (action (R 1)): %lpeg.R, (action (Carg 1)): %lpeg.Carg, - (action (S 1)): %lpeg.S, - (action (Cc 1)): %lpeg.Cc, (action (lpeg re pattern 1)): %re.compile, - (action (lpeg re pattern 1 using 2)): %re.compile - (action (lpeg pattern 1's match of 2)): %lpeg.match - (action (lpeg pattern 1's match of 2 with 3)): [%1, %2, %3] -> (call %lpeg.match with [%1, %2, nil, %3]) + ((P 1)'s meaning):%lpeg.P, ((R 1)'s meaning):%lpeg.R + ((Carg 1)'s meaning):%lpeg.Carg, ((S 1)'s meaning):%lpeg.S + ((Cc 1)'s meaning):%lpeg.Cc, ((lpeg re pattern 1)'s meaning):%re.compile + ((lpeg re pattern 1 using 2)'s meaning):%re.compile + ((lpeg pattern 1's match of 2)'s meaning):%lpeg.match + ((lpeg pattern 1's match of 2 with 3)'s meaning): (..) + [%1, %2, %3] -> (call %lpeg.match with [%1, %2, nil, %3]) %source_code_for_tree = {} %defs = (..) @@ -34,28 +35,30 @@ set {..} %t = (Syntax Tree %t) (Syntax Tree).source_code_for_tree.%t = %userdata.source return %t - ..with fallback %key ->: if: (%key::matches "^ascii_(%d+)$"): %i = (%key::matching "^ascii_(%d+)$") return (call %string.char with [%i as a number]) + (%key::matches "^number_(%d+)$"): %i = (%key::matching "^number_(%d+)$") return (Cc (%i as a number)) -%id_patt = (((P "") - (R "09")) * ((%defs.utf8_char + (R "az") + (R "AZ") + (P "_") + (R "09"))^1 * -1)) -%operator_patt = ((S "'`~!@$^&*+=|<>?/-")^1 * -1) +%id_patt = (..) + ((P "") - (R "09")) * (..) + (%defs.utf8_char + (R "az") + (R "AZ") + (P "_") + (R "09")) ^ 1 * -1 -action [%text is a nomsu id, %text is a nomsu identifier] (..) +%operator_patt = ((S "'`~!@$^&*+=|<>?/-") ^ 1 * -1) +externally [%text is a nomsu id, %text is a nomsu identifier] all mean (..) lpeg pattern %id_patt's match of %text -action [%text is a nomsu operator] (..) +externally (%text is a nomsu operator) means (..) lpeg pattern %operator_patt's match of %text %peg_tidier = (..) lpeg re pattern "\ - file <- %nl* {~ (def/comment) (%nl+ (def/comment))* %nl* ~} + ..file <- %nl* {~ (def/comment) (%nl+ (def/comment))* %nl* ~} def <- anon_def / captured_def anon_def <- ({ident} (" "*) ":" {[^%nl]* (%nl+ " "+ [^%nl]*)*}) @@ -67,18 +70,22 @@ action [%text is a nomsu operator] (..) comment <- "--" [^%nl]* " -action [make parser from %peg] (make parser from %peg using (nil)) - -action [make parser from %peg using %make_tree]: +externally (make parser from %peg) means (make parser from %peg using (nil)) +externally (make parser from %peg using %make_tree) means: %peg = (lpeg pattern %peg_tidier's match of %peg) %peg = (lpeg re pattern %peg using %defs) - local action [%input from %filename parsed]: + (%input from %filename parsed) means: %input = "\%input" - %tree_mt = {__index: {source:%input, filename:%filename}} + %tree_mt = {__index:{source:%input, filename:%filename}} %userdata = {..} - make_tree: %make_tree or ([%]-> (: set %'s metatable to %tree_mt; return %)) + make_tree:%make_tree or ([%] -> (: set %'s metatable to %tree_mt; return %)) filename:%filename, source:%input + %tree = (lpeg pattern %peg's match of %input with %userdata) - assume %tree or barf "File \%filename failed to parse:\n\%input" + assume %tree or barf "\ + ..File \%filename failed to parse: + \%input" + return %tree - return (action (1 from 2 parsed)) + + return ((1 from 2 parsed)'s meaning) -- cgit v1.2.3 From 307dea18815ba4a06a3098edb170d7ad90708815 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 2 Nov 2018 14:38:24 -0700 Subject: Changed stub convention to (foo 1 baz 2) -> foo_1_baz instead of foo_1_baz_2, removed "smext", made some cleanup changes. --- nomnom/parser.nom | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nomnom/parser.nom') diff --git a/nomnom/parser.nom b/nomnom/parser.nom index 13d6112..fe237e8 100644 --- a/nomnom/parser.nom +++ b/nomnom/parser.nom @@ -32,7 +32,7 @@ set {..} %source = (..) Source {filename:%userdata.filename, start:%t.start, stop:%t.stop} set {%t.start: nil, %t.stop: nil, %t.source: %source} - %t = (Syntax Tree %t) + %t = (a Syntax Tree with %t) (Syntax Tree).source_code_for_tree.%t = %userdata.source return %t ..with fallback %key ->: -- cgit v1.2.3