123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- (require 'semantic)
- (declare-function semantic-create-bovine-debug-error-frame
- "semantic/bovine/debug")
- (declare-function semantic-bovine-debug-create-frame
- "semantic/bovine/debug")
- (declare-function semantic-debug-break "semantic/debug")
- (defvar semantic-bovinate-nonterminal-check-obarray nil
- "Obarray of streams already parsed for nonterminal symbols.
- Use this to detect infinite recursion during a parse.")
- (make-variable-buffer-local 'semantic-bovinate-nonterminal-check-obarray)
- (defmacro semantic-lambda (&rest return-val)
- "Create a lambda expression to return a list including RETURN-VAL.
- The return list is a lambda expression to be used in a bovine table."
- `(lambda (vals start end)
- (append ,@return-val (list start end))))
- (defsubst semantic-bovinate-symbol-nonterminal-p (sym table)
- "Return non-nil if SYM is in TABLE, indicating it is NONTERMINAL."
-
- (if (assq sym table) t nil))
- (defmacro semantic-bovinate-nonterminal-db-nt ()
- "Return the current nonterminal symbol.
- Part of the grammar source debugger. Depends on the existing
- environment of `semantic-bovinate-stream'."
- `(if nt-stack
- (car (aref (car nt-stack) 2))
- nonterminal))
- (defun semantic-bovinate-nonterminal-check (stream nonterminal)
- "Check if STREAM not already parsed for NONTERMINAL.
- If so abort because an infinite recursive parse is suspected."
- (or (vectorp semantic-bovinate-nonterminal-check-obarray)
- (setq semantic-bovinate-nonterminal-check-obarray
- (make-vector 13 nil)))
- (let* ((nt (symbol-name nonterminal))
- (vs (symbol-value
- (intern-soft
- nt semantic-bovinate-nonterminal-check-obarray))))
- (if (memq stream vs)
-
- (let ((debug-on-signal t)
- (debug-on-error t))
- (setq semantic-bovinate-nonterminal-check-obarray nil)
- (error "Infinite recursive parse suspected on %s" nt))
- (set (intern nt semantic-bovinate-nonterminal-check-obarray)
- (cons stream vs)))))
- (defun semantic-bovinate-stream (stream &optional nonterminal)
- "Bovinate STREAM, starting at the first NONTERMINAL rule.
- Use `bovine-toplevel' if NONTERMINAL is not provided.
- This is the core routine for converting a stream into a table.
- Return the list (STREAM SEMANTIC-STREAM) where STREAM are those
- elements of STREAM that have not been used. SEMANTIC-STREAM is the
- list of semantic tokens found."
- (if (not nonterminal)
- (setq nonterminal 'bovine-toplevel))
-
- (or semantic--buffer-cache
- (semantic-bovinate-nonterminal-check stream nonterminal))
- (let* ((table semantic--parse-table)
- (matchlist (cdr (assq nonterminal table)))
- (starting-stream stream)
- (nt-loop t)
- nt-popup
- nt-stack
- s
- lse
- lte
- tev
- val
- cvl
- out
- end
- result
- )
- (condition-case debug-condition
- (while nt-loop
- (catch 'push-non-terminal
- (setq nt-popup nil
- end (semantic-lex-token-end (car stream)))
- (while (or nt-loop nt-popup)
- (setq nt-loop nil
- out nil)
- (while (or nt-popup matchlist)
- (if nt-popup
-
- (setq nt-popup nil)
-
- (setq s stream
- cvl nil
- lte (car matchlist)
- )
- (if (or (byte-code-function-p (car lte))
- (listp (car lte)))
-
-
- (setq cvl (list nil))))
- (while (and lte
- (not (byte-code-function-p (car lte)))
- (not (listp (car lte))))
-
- (if (and (boundp 'semantic-debug-enabled)
- semantic-debug-enabled)
- (let* ((db-nt (semantic-bovinate-nonterminal-db-nt))
- (db-ml (cdr (assq db-nt table)))
- (db-mlen (length db-ml))
- (db-midx (- db-mlen (length matchlist)))
- (db-tlen (length (nth db-midx db-ml)))
- (db-tidx (- db-tlen (length lte)))
- (frame (progn
- (require 'semantic/bovine/debug)
- (semantic-bovine-debug-create-frame
- db-nt db-midx db-tidx cvl (car s))))
- (cmd (semantic-debug-break frame))
- )
- (cond ((eq 'fail cmd) (setq lte '(trash 0 . 0)))
- ((eq 'quit cmd) (signal 'quit "Abort"))
- ((eq 'abort cmd) (error "Abort"))
-
- )))
-
- (cond
-
- ((setq nt-loop (assq (car lte) table))
- (setq
-
- nt-stack (cons (vector matchlist cvl lte stream end
- )
- nt-stack)
-
- matchlist (cdr nt-loop)
-
- stream s)
- (throw 'push-non-terminal t)
- )
-
- (t
- (setq lse (car s)
- s (cdr s))
-
- (if (eq (car lte) (semantic-lex-token-class lse))
- (let ((valdot (semantic-lex-token-bounds lse)))
- (setq val (semantic-lex-token-text lse))
- (setq lte (cdr lte))
- (if (stringp (car lte))
- (progn
- (setq tev (car lte)
- lte (cdr lte))
- (if (string-match tev val)
- (setq cvl (cons
- (if (memq (semantic-lex-token-class lse)
- '( semantic-list))
- valdot val)
- cvl))
- (setq lte nil cvl nil)))
- (setq cvl (cons
- (if (memq (semantic-lex-token-class lse)
- '( semantic-list))
- valdot val) cvl)))
- (setq end (semantic-lex-token-end lse))
- )
- (setq lte nil cvl nil))
- )))
- (if (not cvl)
- (setq matchlist (cdr matchlist))
- (let ((start (semantic-lex-token-start (car stream))))
- (setq out (cond
- ((car lte)
- (funcall (car lte)
- (nreverse cvl) start end))
- ((and (= (length cvl) 1)
- (listp (car cvl))
- (not (numberp (car (car cvl)))))
- (append (car cvl) (list start end)))
- (t
-
-
-
- (nreverse (cons end (cons start cvl)))))
- matchlist nil)
- (if (not end)
- (setq out nil)))
-
- ))
- (setq result
- (if (eq s starting-stream)
- (list (cdr s) nil)
- (list s out)))
- (if nt-stack
-
- (let ((state (car nt-stack)))
- (setq nt-popup t
-
- matchlist (aref state 0)
- cvl (aref state 1)
- lte (aref state 2)
- stream (aref state 3)
- end (aref state 4)
-
- nt-stack (cdr nt-stack))
- (if out
- (let ((len (length out))
- (strip (nreverse (cdr (cdr (reverse out))))))
- (setq end (nth (1- len) out)
- cvl (cons strip cvl)
- lte (cdr lte))
- )
-
-
- (setq lte nil cvl nil))
- )))))
- (error
-
- (setq result (list (cdr starting-stream) nil))
- (when (and (boundp 'semantic-debug-enabled)
- semantic-debug-enabled)
- (require 'semantic/bovine/debug)
- (let ((frame (semantic-create-bovine-debug-error-frame
- debug-condition)))
- (semantic-debug-break frame)))))
- result))
- (defalias 'semantic-parse-stream-default 'semantic-bovinate-stream)
- (provide 'semantic/bovine)
|