Skip to content

Commit

Permalink
employ the full parser, as the lexer alone can't properly detect rege…
Browse files Browse the repository at this point in the history
…xps at

beginning of statement (requires
marijnh/parse-js@5fbfeee )
  • Loading branch information
mishoo committed Feb 11, 2011
1 parent 53fe06e commit f92b62d
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/split.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@ never splits *after* a :KEYWORD (in order not to break the JS
semantics, which say that no newline is allowed between return or
throw and their arguments)."
(with-input-from-string (stream code)
(let ((tokenizer (lex-js stream :include-comments t))
(this-token nil)
(let ((this-token nil)
(prev-token nil)
(splits (list 0)))
(labels ((next-token ()
(setf prev-token this-token
this-token (funcall tokenizer)))
(current-length ()
(splits (list 0))
(next-token (lex-js stream :include-comments t)))
(labels ((current-length ()
(- (token-pos this-token) (car splits)))
(split-here ()
(push (token-pos this-token) splits)))
(iter (for i = (next-token))
(until (eq :eof (token-type i)))
(when (and prev-token
(eq :keyword (token-type prev-token)))
(next-iteration))
(when (> (current-length) maxlen)
(when (member (token-type this-token) '(:keyword :atom :name :punc))
(split-here))))
(parse-js (lambda (&rest args)
(setf this-token (apply next-token args))
(unless (and prev-token
(eq :keyword (token-type prev-token)))
(when (> (current-length) maxlen)
(case (token-type this-token)
((:keyword :atom :name :punc) (split-here)))))
(setf prev-token this-token)) :ecma-version 5)
(format nil "~{~A~^~%~}"
(iter (for (from to) on (nreverse splits))
(collect (subseq code from to))))))))
(loop :for (from to) :on (nreverse splits)
:collecting (subseq code from to)))))))

0 comments on commit f92b62d

Please sign in to comment.