fucc / generator / fg-grammar-ll.lisp

#|
 Copyright (c) 2007 Ivan Boldyrev
                                             
 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:
                                             
 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.
                                             
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|#

(cl:in-package #:fucc-generator)


(defparameter +complex-forms-ll+
  '(:* * :+ + :maybe :list :or or))

;;;  Transform complex forms like "(:+ a)" into 3 values:
;;;
;;; 1. symbol that is used in original rule as substitution of the
;;; form ("substitution nterm").  It may be fresh symbol or some old one.
;;;
;;; 2. Set of new rules.  May be empty.
;;;
;;; 3. Value transformer.  This is a symbol naming a function or
;;; lambda-expression (but not result of its evaluation).  Transformer
;;; is applied to value generated by substitution nterm.  It is handy
;;; for efficient implementation of list forms.
(defun expand-form-ll (form rule pos)
  (declare (ignore rule pos))
  (let ((generated-rules '())
        (generated-sym (gensym "SYM")))
    (ecase (first form)
      ((:or or)
       (dolist (subform (rest form))
         (push
          (if (atom subform)
              ;; Atomic
              `(,generated-sym nil ,subform)
              ;; General
              `(,generated-sym nil ,@subform))
          generated-rules))
       (values generated-sym (nreverse generated-rules) nil))
      ((:* *)
       (values generated-sym
               (list
                `(,generated-sym nil (constantly nil))
                `(,generated-sym nil #'list* ,@(rest form) ,generated-sym))
               nil))
      ((:+ +)
       (let ((generated-sym2 (gensym "SYM2")))
        (values generated-sym2
                (list
                 `(,generated-sym2 nil
                       #'list* ,@(rest form) ,generated-sym)
                 `(,generated-sym nil (constantly nil))
                 `(,generated-sym nil #'list* ,@(rest form) ,generated-sym))
                nil)))
      ((:maybe)
       (values generated-sym
                (list
                 `(,generated-sym nil (constantly nil))
                 (if (cddr form)
                     `(,generated-sym nil #'list ,@(rest form))
                     `(,generated-sym nil #'identity ,(second form))))
                 nil))
      ((:list list)
       (let ((generated-sym2 (gensym "SYM3")))
        (destructuring-bind (item delim) (rest form)
          (values generated-sym2
                  (list
                   `(,generated-sym2 nil 
                                     #'cons
                                     ,item ,generated-sym)
                   `(,generated-sym nil (constantly nil))
                   `(,generated-sym nil 
                                    (lambda (second first rest)
                                      (cons first rest))
                                    ,delim ,item ,generated-sym))
                  nil)))))))

(defun expand-rules-ll (rules)
  (declare (type list rules))
  (expand-rules* #'expand-form-ll #'expand-rules-ll +complex-forms-ll+ rules))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.