Compare commits

..

No commits in common. "942f55fb39e304a4333dc78ff8bc6a7cd9d681c7" and "05703a27fabd65862bb13f282883277319479362" have entirely different histories.

2 changed files with 46 additions and 169 deletions

View File

@ -22,9 +22,7 @@
~(set ,name (defn ,name ,;forms))) ~(set ,name (defn ,name ,;forms)))
### Next: a data structure for a parser ### Next: a data structure for a parser
(defn- new-parser (defn- new-parser [tokens]
"Creates a new parser data structure to pass around"
[tokens]
@{ @{
:tokens (tokens :tokens) :tokens (tokens :tokens)
:ast @[] :ast @[]
@ -33,64 +31,42 @@
}) })
### and some helper functions for interfacing with that data structure ### and some helper functions for interfacing with that data structure
(defn- current (defn- current [parser]
"Returns the current token of a parser. If the parser is at the last token, keeps returning the last token." (def curr (get (parser :tokens) (parser :current)))
[parser] (if (not curr)
(def tokens (parser :tokens)) (error "no more tokens")
(get tokens (parser :current) (last tokens))) curr))
(defn- peek (defn- peek [parser] (get (parser :tokens) (inc (parser :current))))
"Returns the next token of the parser. If the parser is at the last token, keeps returning the last token."
[parser]
(def tokens (parser :tokens))
(get tokens (inc (parser :current)) (last tokens)))
(defn- advance (defn- advance [parser] (update parser :current inc))
"Advances the parser by a token"
[parser]
(update parser :current inc))
(defn- type (defn- type [token] (get token :type))
"Returns the type of a token"
[token]
(get token :type))
(defn- check (defn- check [parser type]
"Returns true if the parser's current token is one of the passed types"
[parser type & types]
(def accepts [type ;types])
(def current-type (-> parser current (get :type))) (def current-type (-> parser current (get :type)))
(has-value? accepts current-type)) (= type current-type))
### Parsing functions ### Parsing functions
# forward declarations (declare nonbinding binding synthetic)
(declare simple nonbinding expr toplevel synthetic)
# errors # errors
# terminators are what terminate expressions
(def terminators [:break :newline :semicolon :eof]) (def terminators [:break :newline :semicolon :eof])
(defn- terminates? (defn- terminates? [parser]
"Returns true if the current token in the parser is a terminator"
[parser]
(def curr (current parser)) (def curr (current parser))
(def ttype (type curr)) (def ttype (type curr))
(has-value? terminators ttype)) (has-value? terminators ttype))
# breakers are what terminate panics
(def breaking [:break :newline :semicolon :comma :eof :then :else]) (def breaking [:break :newline :semicolon :comma :eof :then :else])
(defn- breaks? (defn- breaks? [parser]
"Returns true if the current token in the parser should break a panic"
[parser]
(def curr (current parser)) (def curr (current parser))
(def ttype (type curr)) (def ttype (type curr))
(has-value? breaking ttype)) (has-value? breaking ttype))
(defn- panic (defn- panic [parser message]
"Panics the parser: starts skipping tokens until a breaking token is encountered. Adds the error to the parser's errors array, and also errors out." # (print "Panic in the parser: " message)
[parser message]
(print "Panic in the parser: " message)
(def origin (current parser)) (def origin (current parser))
(advance parser) (advance parser)
(def skipped @[origin]) (def skipped @[origin])
@ -98,41 +74,16 @@
(array/push skipped (current parser)) (array/push skipped (current parser))
(advance parser)) (advance parser))
(array/push skipped (current parser)) (array/push skipped (current parser))
# (advance parser)
(def err {:type :error :data skipped :token origin :msg message}) (def err {:type :error :data skipped :token origin :msg message})
(update parser :errors array/push err) (update parser :errors array/push err)
(error err)) (error err))
(defn- expected (defn- expected [parser ttype]
"Panics the parser with a message: expected {type} got ..." (panic parser (string "expected " ttype ", got " (-> parser current type))))
[parser ttype & ttypes]
(def expected (map string [ttype ;ttypes]))
(def type-msg (string/join expected " | "))
(panic parser (string "expected {" type-msg "}, got " (-> parser current type))))
(defn- expect (defn- expect [parser type]
"Panics if the parser's current token is not of type; otherwise does nothing & returns nil" (if-not (check parser type) (expected parser type)))
[parser type & types]
(if-not (check parser type ;types) (expected parser type ;types)))
(defn- expect-ret
"Same as expect, but captures the error, returning it as a value"
[parser type]
(try (expect parser type) ([e] e)))
(defn- capture
"Applies the parse function to the parser, returning the parsed AST. If there is a panic, captures the panic and returns it as a value."
[parse-fn parser]
(try (parse-fn parser) ([e] e)))
(defn- accept-one
"Accepts a single token of passed type, advancing the parser if a match, doing nothing if not."
[parser type]
(if (check parser type) (advance parser)))
(defn- accept-many
"Accepts any number of tokens of a passed type, advancing the parser on match until there are no more matches. Does nothing on no match."
[parser type]
(while (check parser type) (advance parser)))
# atoms # atoms
(defn- bool [parser] (defn- bool [parser]
@ -269,7 +220,7 @@
(advance parser) (advance parser)
ast) ast)
(defn- sett [parser] (defn- set [parser]
(def origin (current parser)) (def origin (current parser))
(advance parser) (advance parser)
(def ast {:type :set :data @[] :token origin}) (def ast {:type :set :data @[] :token origin})
@ -310,75 +261,6 @@
(advance parser) (advance parser)
ast) ast)
### conditional forms
# if {simple} then {nonbinding} else {nonbinding}
(defn- iff [parser]
(def ast {:type :if :data @[] :token (current parser)})
(advance parser) #consume the if
(array/push (ast :data) (capture simple parser))
(accept-many parser :newline)
(if-let [err (expect-ret parser :then)]
(array/push (ast :data) err)
(advance parser))
(array/push (ast :data) (capture nonbinding parser))
(accept-many parser :newline)
(if-let [err (expect-ret parser :else)]
(array/push (ast :data) err)
(advance parser))
(array/push (ast :data) (capture nonbinding parser))
ast)
### XXX: We've got an off-by-one error here
# We're expecting a terminator, we panic until we get to a terminator, and we then return back to something that expects a terminator, and now we've started parsing again *at the terminator*
(defn- terminator [parser]
(if-not (terminates? parser)
# this line panics, captures the panic, advances the parser, and re-throws the error
(try (panic parser "expected terminator") ([e] (advance parser) (error e))))
(advance parser)
(while (terminates? parser) (advance parser)))
# {simple} -> {nonbinding} {terminator}
(defn- when-clause [parser]
(def clause @[])
(print "parsing lhs: " (-> parser current type))
(array/push clause (capture simple parser))
(print "parsing arrow")
(if-let [err (expect-ret parser :arrow)]
(array/push clause err)
(advance parser))
(print "accepting newlines")
(accept-many parser :newline)
(print "parsing rhs")
(array/push clause (nonbinding parser))
(print "parsing terminator")
(try (terminator parser) ([e] (array/push clause e)))
clause)
(defn- whenn [parser]
(def ast {:type :when :data @[] :origin (current parser)})
(advance parser) # consume cond
(if-let [err (expect-ret parser :lbrace)]
(do
(array/push (ast :data) err)
(break ast))
(advance parser))
(accept-many parser :newline)
(while (not (check parser :rbrace :eof))
(array/push (ast :data) (capture when-clause parser)))
(advance parser)
ast)
(defn- match [parser])
### function forms
(defn- fnn [parser])
(defn- lambda [parser])
### compoound forms
(defn- block [parser])
(defn- doo [parser])
### expressions ### expressions
# four levels of expression complexity: # four levels of expression complexity:
@ -433,8 +315,8 @@
:word (word parser) :word (word parser)
# conditional forms # conditional forms
:if (iff parser) :if (unreachable)
:when (whenn parser) :cond (unreachable)
:match (unreachable) :match (unreachable)
:with (unreachable) :with (unreachable)
@ -454,9 +336,9 @@
(defrec expr [parser] (defrec expr [parser]
(def curr (current parser)) (def curr (current parser))
(case (type curr) (case (type curr)
:let (unreachable) :let nil
:fn (unreachable) :fn nil
:ref (unreachable) :ref nil
:nil (nill parser) :nil (nill parser)
:true (bool parser) :true (bool parser)
:false (bool parser) :false (bool parser)
@ -468,8 +350,8 @@
:startdict (dict parser) :startdict (dict parser)
:startset (sett parser) :startset (sett parser)
:word (word parser) :word (word parser)
:if (iff parser) :if (unreachable)
:when (whenn parser) :cond (unreachable)
:match (unreachable) :match (unreachable)
:with (unreachable) :with (unreachable)
:do (unreachable) :do (unreachable)
@ -479,16 +361,16 @@
) )
(defrec toplevel [parser] (defrec toplevel [parser]
(def when (current parser)) (def curr (current parser))
(case (type curr) (case (type curr)
:pkg (unreachable) :pkg nil
:ns (unreachable) :ns nil
:test (unreachable) :test nil
:import (unreachable) :import nil
:use (unreachable) :use nil
:let (unreachable) :let nil
:fn (unreachable) :fn nil
:ref (unreachable) :ref nil
:nil (nill parser) :nil (nill parser)
:true (bool parser) :true (bool parser)
:false (bool parser) :false (bool parser)
@ -500,8 +382,8 @@
:startdict (dict parser) :startdict (dict parser)
:startset (sett parser) :startset (sett parser)
:word (word parser) :word (word parser)
:if (iff parser) :if (unreachable)
:when (whenn parser) :cond (unreachable)
:match (unreachable) :match (unreachable)
:with (unreachable) :with (unreachable)
:do (unreachable) :do (unreachable)
@ -510,20 +392,15 @@
) )
) )
(os/cd "janet") # when repl to do relative imports (os/cd "janet") # For repl to do relative imports
(import ./scanner :as s) (import ./scanner :as s)
(do (do
#(comment #(comment
(def source `when {
foo -> bar quux (def scanned (s/scan "#{}"))
bar -> baz
}
`)
(def scanned (s/scan source))
(def a-parser (new-parser scanned)) (def a-parser (new-parser scanned))
(def parsed (whenn a-parser)) (def parsed (nonbinding a-parser))
(-> parsed) (-> parsed)
# (map (fn [t] (t :type)) (scanned :tokens))
) )

View File

@ -259,12 +259,12 @@
"\n" (add-token (update scanner :line inc) :newline) "\n" (add-token (update scanner :line inc) :newline)
"\\" (add-token scanner :backslash) "\\" (add-token scanner :backslash)
"=" (add-token scanner :equals) "=" (add-token scanner :equals)
">" (add-token (add-token scanner :break) :pipeline) ">" (add-token scanner :pipeline)
## two-character tokens ## two-character tokens
## -> ## ->
"-" (cond "-" (cond
(= next ">") (add-token (advance scanner) :arrow) (= next ">") (add-token (advance scanner) :rarrow)
(digit? next) (add-number char scanner) (digit? next) (add-number char scanner)
:else (add-error scanner (string "Expected -> or negative number after `-`. Got `" char next "`"))) :else (add-error scanner (string "Expected -> or negative number after `-`. Got `" char next "`")))