Update CEDET from upstream.
[bpt/emacs.git] / lisp / cedet / semantic / wisent / javascript.el
CommitLineData
bb051423
CY
1;;; semantic/wisent/javascript.el --- javascript parser support
2
acaf905b 3;; Copyright (C) 2005, 2009-2012 Free Software Foundation, Inc.
bb051423
CY
4
5;; Author: Eric Ludlam <zappo@gnu.org>
6;; Keywords: syntax
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23;;; Commentary:
24;;
25;; Parser support for javascript language.
26
27
28;;; Code:
29(require 'semantic/java)
30(require 'semantic/wisent)
31(require 'semantic/wisent/js-wy)
32
33(defun wisent-javascript-jv-expand-tag (tag)
34 "Expand TAG into a list of equivalent tags, or nil.
35Expand multiple variable declarations in the same statement, that is
36tags of class `variable' whose name is equal to a list of elements of
37the form (NAME VALUE START . END). NAME is a variable name. VALUE is
38an initializer START and END are the bounds in the declaration, related
39to this variable NAME."
40 (let (elts elt value clone start end xpand)
41 (when (and (eq 'variable (semantic-tag-class tag))
42 (consp (setq elts (semantic-tag-name tag))))
43 ;; There are multiple names in the same variable declaration.
44 (while elts
45 ;; For each name element, clone the initial tag and give it
46 ;; the name of the element.
47 (setq elt (car elts)
48 elts (cdr elts)
49 clone (semantic-tag-clone tag (car elt))
50 value (car (cdr elt))
6583ba0e
CY
51 start (if elts (car (cddr elt)) (semantic-tag-start tag))
52 end (if xpand (cdr (cddr elt)) (semantic-tag-end tag))
bb051423 53 xpand (cons clone xpand))
62a81506
CY
54 ;; Set the definition of the cloned tag
55 (semantic-tag-put-attribute clone :default-value value)
bb051423
CY
56 ;; Set the bounds of the cloned tag with those of the name
57 ;; element.
58 (semantic-tag-set-bounds clone start end))
59 xpand)))
60
61;;; Override Methods
62;;
63;; These methods override aspects of how semantic-tools can access
64;; the tags created by the javascript parser.
65;; Local context
4f306b0f 66(define-mode-local-override semantic-get-local-variables
bb051423
CY
67 javascript-mode ()
68 "Get local values from a specific context.
69This function overrides `get-local-variables'."
70 ;; Does javascript have identifiable local variables?
71 nil)
72
62a81506
CY
73(define-mode-local-override semantic-tag-protection javascript-mode (tag &optional parent)
74 "Return protection information about TAG with optional PARENT.
75This function returns on of the following symbols:
76 nil - No special protection. Language dependent.
77 'public - Anyone can access this TAG.
78 'private - Only methods in the local scope can access TAG.
79 'protected - Like private for outside scopes, like public for child
80 classes.
81Some languages may choose to provide additional return symbols specific
82to themselves. Use of this function should allow for this.
bb051423 83
62a81506
CY
84The default behavior (if not overridden with `tag-protection'
85is to return a symbol based on type modifiers."
86 nil)
87
88(define-mode-local-override semantic-analyze-scope-calculate-access javascript-mode (type scope)
89 "Calculate the access class for TYPE as defined by the current SCOPE.
90Access is related to the :parents in SCOPE. If type is a member of SCOPE
91then access would be 'private. If TYPE is inherited by a member of SCOPE,
92the access would be 'protected. Otherwise, access is 'public."
93 nil)
94
95(define-mode-local-override semantic-ctxt-current-symbol javascript-mode (&optional point)
96 "Return the current symbol the cursor is on at POINT in a list.
97This is a very simple implementation for Javascript symbols. It
98will at maximum do one split, so that the first part is seen as
99one type. For example: $('#sel').foo.bar will return (\"$('sel').foo\" \"bar\").
100This is currently needed for the mozrepl omniscient database."
101 (save-excursion
102 (if point (goto-char point))
103 (let* ((case-fold-search semantic-case-fold)
104 symlist tmp end)
105 (with-syntax-table semantic-lex-syntax-table
106 (save-excursion
107 (when (looking-at "\\w\\|\\s_")
108 (forward-sexp 1))
109 (setq end (point))
110 (unless (re-search-backward "\\s-" (point-at-bol) t)
111 (beginning-of-line))
112 (setq tmp (buffer-substring-no-properties (point) end))
113 (if (string-match "\\(.+\\)\\." tmp)
114 (setq symlist (list (match-string 1 tmp)
115 (substring tmp (1+ (match-end 1)) (length tmp))))
116 (setq symlist (list tmp))))))))
117
bb051423
CY
118;;; Setup Function
119;;
62a81506
CY
120;; Since javascript-mode is an alias for js-mode, let it inherit all
121;; the overrides.
122(define-child-mode js-mode javascript-mode)
bb051423 123
da418981
DE
124;; Since javascript-mode is an alias for js-mode, let it inherit all
125;; the overrides.
126(define-child-mode js-mode javascript-mode)
127
f3628edd
GM
128;; In semantic-imenu.el, not part of Emacs.
129(defvar semantic-imenu-summary-function)
130
bb051423
CY
131;;;###autoload
132(defun wisent-javascript-setup-parser ()
133 "Setup buffer for parse."
134 (wisent-javascript-jv-wy--install-parser)
135 (setq
136 ;; Lexical Analysis
137 semantic-lex-analyzer 'javascript-lexer-jv
138 semantic-lex-number-expression semantic-java-number-regexp
139 ;; semantic-lex-depth nil ;; Full lexical analysis
140 ;; Parsing
141 semantic-tag-expand-function 'wisent-javascript-jv-expand-tag
142 ;; Environment
143 semantic-imenu-summary-function 'semantic-format-tag-name
144 imenu-create-index-function 'semantic-create-imenu-index
145 semantic-command-separation-character ";"
146 ))
147
bb051423
CY
148(provide 'semantic/wisent/javascript-jv)
149
150;; Local variables:
151;; generated-autoload-file: "../loaddefs.el"
bb051423
CY
152;; generated-autoload-load-name: "semantic/wisent/javascript"
153;; End:
154
155;;; semantic/wisent/javascript-jv.el ends here