123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- (require 'semantic/wisent)
- (require 'semantic/wisent/python-wy)
- (require 'semantic/dep)
- (require 'semantic/ctxt)
- (defconst wisent-python-string-re
- (concat (regexp-opt '("r" "u" "ur" "R" "U" "UR" "Ur" "uR") t)
- "?['\"]")
- "Regexp matching beginning of a Python string.")
- (defvar wisent-python-EXPANDING-block nil
- "Non-nil when expanding a paren block for Python lexical analyzer.")
- (defun wisent-python-implicit-line-joining-p ()
- "Return non-nil if implicit line joining is active.
- That is, if inside an expression in parentheses, square brackets or
- curly braces."
- wisent-python-EXPANDING-block)
- (defsubst wisent-python-forward-string ()
- "Move point at the end of the Python string at point."
- (when (looking-at wisent-python-string-re)
-
- (and (match-end 1) (goto-char (match-end 1)))
-
- (cond
- ((looking-at "\"\"\"[^\"]")
- (search-forward "\"\"\"" nil nil 2))
- ((looking-at "'''[^']")
- (search-forward "'''" nil nil 2))
- ((forward-sexp 1)))))
- (defun wisent-python-forward-line ()
- "Move point to the beginning of the next logical line.
- Usually this is simply the next physical line unless strings,
- implicit/explicit line continuation, blank lines, or comment lines are
- encountered. This function skips over such items so that the point is
- at the beginning of the next logical line. If the current logical
- line ends at the end of the buffer, leave the point there."
- (while (not (eolp))
- (when (= (point)
- (progn
- (cond
-
- ((looking-at wisent-python-string-re)
- (wisent-python-forward-string))
-
- ((looking-at "\\s<")
- (end-of-line))
-
- ((looking-at "\\(\\s(\\|\\s\"\\)")
- (forward-sexp 1))
-
-
- ((looking-at "\\s\\")
- (forward-line 1))
-
-
- ((skip-syntax-forward "-w_.$)")))
- (point)))
- (error "python-forward-line endless loop detected")))
-
- (forward-comment (point-max))
-
- (or (eobp) (beginning-of-line)))
- (defun wisent-python-forward-line-skip-indented ()
- "Move point to the next logical line, skipping indented lines.
- That is the next line whose indentation is less than or equal to the
- indentation of the current line."
- (let ((indent (current-indentation)))
- (while (progn (wisent-python-forward-line)
- (and (not (eobp))
- (> (current-indentation) indent))))))
- (defun wisent-python-end-of-block ()
- "Move point to the end of the current block."
- (let ((indent (current-indentation)))
- (while (and (not (eobp)) (>= (current-indentation) indent))
- (wisent-python-forward-line-skip-indented))
-
- (forward-comment (- (point-max)))
- (or (bolp) (forward-line 1))
- ))
- (defvar wisent-python-indent-stack)
- (define-lex-analyzer wisent-python-lex-beginning-of-line
- "Detect and create Python indentation tokens at beginning of line."
- (and
- (bolp) (not (wisent-python-implicit-line-joining-p))
- (let ((last-indent (car wisent-python-indent-stack))
- (last-pos (point))
- (curr-indent (current-indentation)))
- (skip-syntax-forward "-")
- (cond
-
- ((or (eolp) (looking-at semantic-lex-comment-regex))
- (forward-comment (point-max))
- (or (eobp) (beginning-of-line))
- (setq semantic-lex-end-point (point))
-
- t)
-
- ((= curr-indent last-indent)
- (setq semantic-lex-end-point (point))
-
- nil)
-
- ((> curr-indent last-indent)
- (if (or (not semantic-lex-maximum-depth)
- (< semantic-lex-current-depth semantic-lex-maximum-depth))
- (progn
-
- (setq semantic-lex-current-depth (1+ semantic-lex-current-depth))
- (push curr-indent wisent-python-indent-stack)
- (semantic-lex-push-token
- (semantic-lex-token 'INDENT last-pos (point))))
-
- (semantic-lex-push-token
- (semantic-lex-token
- 'INDENT_BLOCK
- (progn (beginning-of-line) (point))
- (semantic-lex-unterminated-syntax-protection 'INDENT_BLOCK
- (wisent-python-end-of-block)
- (point)))))
-
- t)
-
- (t
-
- (while (< curr-indent last-indent)
- (pop wisent-python-indent-stack)
- (setq semantic-lex-current-depth (1- semantic-lex-current-depth)
- last-indent (car wisent-python-indent-stack))
- (semantic-lex-push-token
- (semantic-lex-token 'DEDENT last-pos (point))))
-
-
- (/= last-pos (point))))))
-
- )
- (define-lex-regex-analyzer wisent-python-lex-end-of-line
- "Detect and create Python newline tokens.
- Just skip the newline character if the following line is an implicit
- continuation of current line."
- "\\(\n\\|\\s>\\)"
- (if (wisent-python-implicit-line-joining-p)
- (setq semantic-lex-end-point (match-end 0))
- (semantic-lex-push-token
- (semantic-lex-token 'NEWLINE (point) (match-end 0)))))
- (define-lex-regex-analyzer wisent-python-lex-string
- "Detect and create python string tokens."
- wisent-python-string-re
- (semantic-lex-push-token
- (semantic-lex-token
- 'STRING_LITERAL
- (point)
- (semantic-lex-unterminated-syntax-protection 'STRING_LITERAL
- (wisent-python-forward-string)
- (point)))))
- (define-lex-regex-analyzer wisent-python-lex-ignore-backslash
- "Detect and skip over backslash (explicit line joining) tokens.
- A backslash must be the last token of a physical line, it is illegal
- elsewhere on a line outside a string literal."
- "\\s\\\\s-*$"
-
-
- (forward-line)
- (skip-syntax-forward "-")
- (setq semantic-lex-end-point (point)))
- (define-lex wisent-python-lexer
- "Lexical Analyzer for Python code."
-
- wisent-python-lex-beginning-of-line
- wisent-python-lex-end-of-line
-
- wisent-python-lex-string
-
- wisent-python-wy--<number>-regexp-analyzer
- wisent-python-wy--<keyword>-keyword-analyzer
- wisent-python-wy--<symbol>-regexp-analyzer
- wisent-python-wy--<block>-block-analyzer
- wisent-python-wy--<punctuation>-string-analyzer
-
- wisent-python-lex-ignore-backslash
- semantic-lex-ignore-whitespace
- semantic-lex-ignore-comments
-
- semantic-lex-default-action)
- (define-mode-local-override semantic-lex python-mode
- (start end &optional depth length)
- "Lexically analyze Python code in current buffer.
- See the function `semantic-lex' for the meaning of the START, END,
- DEPTH and LENGTH arguments.
- This function calls `wisent-python-lexer' to actually perform the
- lexical analysis, then emits the necessary Python DEDENT tokens from
- what remains in the `wisent-python-indent-stack'."
- (let* ((wisent-python-indent-stack (list 0))
- (stream (wisent-python-lexer start end depth length))
- (semantic-lex-token-stream nil))
-
- (while (> (pop wisent-python-indent-stack) 0)
- (semantic-lex-push-token (semantic-lex-token 'DEDENT end end)))
- (nconc stream (nreverse semantic-lex-token-stream))))
- (define-mode-local-override semantic-get-local-variables python-mode ()
- "Get the local variables based on point's context.
- To be implemented for Python! For now just return nil."
- nil)
- (defcustom-mode-local-semantic-dependency-system-include-path
- python-mode semantic-python-dependency-system-include-path
- nil
- "The system include path used by Python language.")
- (defun wisent-python-default-setup ()
- "Setup buffer for parse."
- (wisent-python-wy--install-parser)
- (set (make-local-variable 'parse-sexp-ignore-comments) t)
- (setq
-
- semantic-type-relation-separator-character '(".")
- semantic-command-separation-character ";"
-
-
-
-
-
-
- imenu-create-index-function 'semantic-create-imenu-index
-
- semantic-symbol->name-assoc-list-for-type-parts '((variable . "Variables")
- (function . "Methods"))
- semantic-symbol->name-assoc-list '((type . "Classes")
- (variable . "Variables")
- (function . "Functions")
- (include . "Imports")
- (package . "Package")
- (code . "Code")))
- )
- (add-hook 'python-mode-hook 'wisent-python-default-setup)
- (define-child-mode python-2-mode python-mode "Python 2 mode")
- (define-child-mode python-3-mode python-mode "Python 3 mode")
- (defun wisent-python-lex-buffer ()
- "Run `wisent-python-lexer' on current buffer."
- (interactive)
- (semantic-lex-init)
- (let ((token-stream (semantic-lex (point-min) (point-max) 0)))
- (with-current-buffer (get-buffer-create "*wisent-python-lexer*")
- (erase-buffer)
- (pp token-stream (current-buffer))
- (goto-char (point-min))
- (pop-to-buffer (current-buffer)))))
- (provide 'semantic/wisent/python)
|