Optimize incremental parser by skipping over sequential untainted tokens

WIP-incremental-parsing
Hugo Thunnissen 10 months ago
parent 67fd01e68d
commit fa2967ddba

@ -69,8 +69,8 @@
(and (<= (phpinspect-region-start reg1) (phpinspect-region-start reg2))
(>= (phpinspect-region-end reg1) (phpinspect-region-end reg2))))
(defsubst phpinspect-make-meta (parent start end whitespace-before token &optional overlay)
(list 'meta parent start end whitespace-before token overlay))
(defsubst phpinspect-make-meta (parent start end whitespace-before token &optional overlay right-siblings)
(list 'meta parent start end whitespace-before token overlay right-siblings))
(defsubst phpinspect-meta-parent (meta)
(cadr meta))
@ -79,6 +79,10 @@
(gv-define-setter phpinspect-meta-start (start meta) `(setcar (cddr ,meta) ,start))
(gv-define-setter phpinspect-meta-overlay (overlay meta) `(setcar (nthcdr 6 ,meta) ,overlay))
(gv-define-setter phpinspect-meta-parent (parent meta) `(setcar (cdr ,meta) ,parent))
(gv-define-setter phpinspect-meta-right-siblings (siblings meta) `(setcar (nthcdr 7 ,meta) ,siblings))
(defsubst phpinspect-meta-right-siblings (meta)
(car (nthcdr 7 meta)))
(defsubst phpinspect-meta-overlay (meta)
(car (nthcdr 6 meta)))
@ -204,7 +208,8 @@
(when (and last-token-start
(<= start last-token-start))
(let ((child)
(stack (phpinspect-bmap-token-stack bmap)))
(stack (phpinspect-bmap-token-stack bmap))
(right-siblings))
(while (and (car stack) (>= (phpinspect-meta-start (car stack))
start))
@ -214,7 +219,15 @@
(setf (phpinspect-meta-parent
(phpinspect-overlay-token-meta
(phpinspect-meta-overlay child)))
token-meta)))
token-meta))
(setf (phpinspect-meta-right-siblings child) right-siblings)
(when (phpinspect-meta-overlay child)
(setf (phpinspect-meta-right-siblings
(phpinspect-overlay-token-meta
(phpinspect-meta-overlay child)))
right-siblings))
(push (phpinspect-meta-token child) right-siblings))
(setf (phpinspect-bmap-token-stack bmap) stack)))

@ -38,17 +38,22 @@
(defsubst phpinspect-taint-iterator-current (iter)
(car iter))
(defsubst phpinspect-taint-iterator-follow (iter pos)
(or (while (and (phpinspect-taint-iterator-current iter)
(> pos (phpinspect-taint-end
(phpinspect-taint-iterator-current iter))))
(setf (phpinspect-taint-iterator-current iter) (pop (cdr iter))))
(phpinspect-taint-iterator-current iter)))
(defsubst phpinspect-taint-iterator-token-is-tainted-p (iter meta)
(when (phpinspect-taint-iterator-current iter)
(while (and (phpinspect-taint-iterator-current iter)
(> (phpinspect-meta-start meta)
(phpinspect-taint-end
(phpinspect-taint-iterator-current iter))))
(setf (phpinspect-taint-iterator-current iter) (pop (cdr iter))))
(and (phpinspect-taint-iterator-follow iter (phpinspect-meta-start meta))
(phpinspect-taint-overlaps-meta
(phpinspect-taint-iterator-current iter) meta)))
(and (phpinspect-taint-iterator-current iter)
(phpinspect-taint-overlaps-meta
(phpinspect-taint-iterator-current iter) meta))))
(defsubst phpinspect-taint-iterator-region-is-tainted-p (iter start end)
(and (phpinspect-taint-iterator-follow iter start)
(phpinspect-taint-overlaps-region
(phpinspect-taint-iterator-current iter) start end)))
(defsubst phpinspect-edit-original-end (edit)
(or (caar edit) 0))
@ -122,6 +127,14 @@
(and (> (phpinspect-taint-end taint) point)
(<= (phpinspect-taint-start taint) point)))
(defsubst phpinspect-taint-overlaps-region (taint start end)
(or (phpinspect-taint-overlaps-point taint start)
(phpinspect-taint-overlaps-point taint end)
(and (> end (phpinspect-taint-start taint))
(<= start (phpinspect-taint-start taint)))
(and (> end (phpinspect-taint-end taint))
(<= start (phpinspect-taint-end taint)))))
(defsubst phpinspect-taint-overlaps (taint1 taint2)
(or (phpinspect-taint-overlaps-point taint1 (phpinspect-taint-start taint2))
(phpinspect-taint-overlaps-point taint1 (phpinspect-taint-end taint2))

@ -584,7 +584,7 @@ parsing. Usually used in combination with
delimiter-predicate)))
`(lambda (context buffer max-point &optional continue-condition root)
(with-current-buffer buffer
(let* ((tokens)
(let* ((tokens (list ,tree-type))
(root-start (point))
(bmap (phpinspect-pctx-bmap context))
(previous-bmap (phpinspect-pctx-previous-bmap context))
@ -622,10 +622,24 @@ parsing. Usually used in combination with
;; Re-register existing token
(let ((delta (- start-position original-position)))
(phpinspect-bmap-overlay
bmap previous-bmap existing-meta delta
(phpinspect-pctx-consume-whitespace context)))
(phpinspect-bmap-overlay
bmap previous-bmap existing-meta (- start-position original-position)
(phpinspect-pctx-consume-whitespace context))
;; Check if we can fast-forward to more siblings
(when (phpinspect-meta-right-siblings existing-meta)
(dolist (sibling (phpinspect-meta-right-siblings existing-meta))
(setq existing-meta (phpinspect-bmap-token-meta previous-bmap sibling))
(unless (phpinspect-taint-iterator-region-is-tainted-p
taint-iterator current-end-position (phpinspect-meta-end existing-meta))
(nconc tokens (list token))
(setq token (phpinspect-meta-token existing-meta))
(phpinspect-bmap-overlay
bmap previous-bmap existing-meta (- start-position original-position)
(phpinspect-pctx-consume-whitespace context))
(setq current-end-position (phpinspect-edtrack-current-position-at-point
edtrack (phpinspect-meta-end existing-meta))))))
;;(message "Current pos: %d, end pos: %d" (point) current-end-position)
(goto-char current-end-position)
@ -643,12 +657,8 @@ parsing. Usually used in combination with
handlers)
(t (forward-char)))
(when token
(if (null tokens)
(setq tokens (list token))
(progn
(nconc tokens (list token))))
(nconc tokens (list token))
(setq token nil))))
(push ,tree-type tokens)
(when root
(phpinspect-pctx-register-token context tokens root-start (point)))

Loading…
Cancel
Save