From 122cd9509e433c57e037d2cdc73ac7b295d96636 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 8 Aug 2019 11:17:14 +0100 Subject: [PATCH] src/parser/parse-expr-{proto,impl}.lisp: Fully hide the parser state. We didn't publish the ability to pop things, so external implementers are rather limited in their options. Withdraw this entirely. The `push-operator' and `apply-operator' generic functions are now internal. The `push-value' function is gone entirely: the only caller was `parse-expression', and that now simply pushes the value onto the stack directly. --- doc/SYMBOLS | 14 -------------- doc/parsing.tex | 9 --------- src/parser/parser-expr-impl.lisp | 19 ++++++++++++++----- src/parser/parser-expr-proto.lisp | 27 +-------------------------- 4 files changed, 15 insertions(+), 54 deletions(-) diff --git a/doc/SYMBOLS b/doc/SYMBOLS index 4de0860..33d4b95 100644 --- a/doc/SYMBOLS +++ b/doc/SYMBOLS @@ -1706,7 +1706,6 @@ floc-proto.lisp with-default-error-location macro parser-expr-proto.lisp - apply-operator generic binop macro close-parenthesis class expr parser-form @@ -1719,8 +1718,6 @@ parser-expr-proto.lisp postop macro prefix-operator class preop macro - push-operator generic - push-value generic rparen function simple-binary-operator class simple-operator class @@ -1996,10 +1993,6 @@ cl:t position-aware-input-stream [position-aware-stream] Methods: -apply-operator - open-parenthesis sod-parser::expression-parse-state - simple-binary-operator sod-parser::expression-parse-state - simple-unary-operator sod-parser::expression-parse-state charbuf-scanner-map charbuf-scanner t classify-condition @@ -2148,13 +2141,6 @@ position-aware-stream-line cl:print-object file-location t simple-operator t -push-operator - t sod-parser::expression-parse-state - close-parenthesis sod-parser::expression-parse-state - open-parenthesis sod-parser::expression-parse-state [:after] - prefix-operator sod-parser::expression-parse-state -push-value - t sod-parser::expression-parse-state scanner-at-eof-p charbuf-scanner list-scanner diff --git a/doc/parsing.tex b/doc/parsing.tex index 0d9bf04..22960ee 100644 --- a/doc/parsing.tex +++ b/doc/parsing.tex @@ -819,15 +819,6 @@ file-location protocols. \subsection{Expression parsing} \label{sec:parsing.syntax.expression} -\begin{describe}{gf}{push-operator @ @} -\end{describe} - -\begin{describe}{gf}{push-value @ @} -\end{describe} - -\begin{describe}{gf}{apply-operator @ @} -\end{describe} - \begin{describe}{gf}{operator-push-action @ @} \end{describe} diff --git a/src/parser/parser-expr-impl.lisp b/src/parser/parser-expr-impl.lisp index 16e0c53..d4bc3a0 100644 --- a/src/parser/parser-expr-impl.lisp +++ b/src/parser/parser-expr-impl.lisp @@ -26,7 +26,7 @@ (cl:in-package #:sod-parser) ;;;-------------------------------------------------------------------------- -;;; Basic protocol implementation. +;;; Basic protocol. (defclass expression-parse-state () ((opstack :initform nil :type list) @@ -35,9 +35,18 @@ (:documentation "State for the expression parser. Largely passive.")) -(defmethod push-value (value (state expression-parse-state)) - (with-slots (valstack) state - (push value valstack))) +(defgeneric push-operator (operator state) + (:documentation + "Push an OPERATOR onto the STATE's operator stack. + + This should apply existing stacked operators as necessary to obey the + language's precedence rules.")) + +(defgeneric apply-operator (operator state) + (:documentation + "Apply the OPERATOR to arguments on the STATE's value stack. + + This should pop any necessary arguments, and push the result.")) (defmethod push-operator (operator (state expression-parse-state)) (with-slots (opstack) state @@ -204,7 +213,7 @@ (push-operator value state))) (multiple-value-bind (value winp) (parse p-operand) (unless winp (fail value)) - (push-value value state)) + (push value (slot-value state 'valstack))) (loop (multiple-value-bind (value winp) (parse p-postop) (unless winp (return)) (push-operator value state))))) diff --git a/src/parser/parser-expr-proto.lisp b/src/parser/parser-expr-proto.lisp index 326f3e5..9052e54 100644 --- a/src/parser/parser-expr-proto.lisp +++ b/src/parser/parser-expr-proto.lisp @@ -28,29 +28,6 @@ ;;;-------------------------------------------------------------------------- ;;; Basic protocol. -(export 'push-operator) -(defgeneric push-operator (operator state) - (:documentation - "Push an OPERATOR onto the STATE's operator stack. - - This should apply existing stacked operators as necessary to obey the - language's precedence rules.")) - -(export 'push-value) -(defgeneric push-value (value state) - (:documentation - "Push VALUE onto the STATE's value stack. - - The default method just does that without any fuss. It's unlikely that - this will need changing unless you invent some really weird values.")) - -(export 'apply-operator) -(defgeneric apply-operator (operator state) - (:documentation - "Apply the OPERATOR to argument on the STATE's value stack. - - This should pop any necessary arguments, and push the result.")) - (export 'operator-push-action) (defgeneric operator-push-action (left right) (:documentation @@ -149,9 +126,7 @@ Prefix operators are special because they are pushed at a time when the existing topmost operator on the stack may not have its operand available. It is therefore incorrect to attempt to apply any existing - operators without careful checking. This class provides a method on - `push-operator' which immediately pushes the new operator without - inspecting the existing stack.")) + operators without careful checking.")) (export 'simple-operator) (defclass simple-operator () -- 2.11.0