patch: javascript support

vova gorsunov <[email protected]> Wed, 17 Mar 2010 16:33:51 +0300
Newsgroups gmane.comp.gnu.global.general
Message-ID <[email protected]>
Hi, the following code catches JavaScript function definitions. Feel
free to tell me anything about it.

Index: libparser/js.c
===================================================================
RCS file: libparser/js.c
diff -N libparser/js.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libparser/js.c	17 Mar 2010 06:51:29 -0000
@@ -0,0 +1,71 @@
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#else
+#include <strings.h>
+#endif
+
+#include "internal.h"
+#include "die.h"
+#include "strbuf.h"
+#include "strlimcpy.h"
+#include "token.h"
+#include "../gtags-parser/js_res.h"
+
+/*
+ */
+void
+javaScript(const struct parser_param *param)
+{
+	int cc;
+	const char *interested = "()=:";
+	char lastToken[MAXTOKEN];
+	int level; // for PUT
+
+
+	if (!opentoken(param->file))
+		die("'%s' cannot open.", param->file);
+	//crflag = 1;			/* require '\n' as a token */
+
+	while ((cc = nexttoken(interested, js_reserved_word)) != EOF) {
+		switch (cc) {
+		case SYMBOL:	
+			cc = peekc(0);
+			if('=' == cc || ':' == cc) {
+				strlimcpy(lastToken, token, sizeof lastToken);
+				nexttoken(interested, js_reserved_word);
+				cc = nexttoken(interested, js_reserved_word);
+				if(JS_FUNCTION == cc) {
+					PUT(PARSER_DEF, lastToken, lineno, sp);
+					pushbacktoken();
+					break;
+				} else {
+					pushbacktoken();
+					break;
+				}
+			}
+
+			if(SYMBOL == cc)
+				PUT(PARSER_REF_SYM, token, lineno, sp);
+			break;
+
+		case JS_FUNCTION:
+			if(peekc(0) != '(') {
+				cc = nexttoken(interested, js_reserved_word);
+				if(SYMBOL == cc) {
+					PUT(PARSER_DEF, token, lineno, sp);
+				}
+			}
+			break;
+		}
+
+		if(EOF == cc)
+			break;
+	}
+
+	closetoken();
+}
+			
Index: libparser/Makefile.am
===================================================================
RCS file: /sources/global/global/libparser/Makefile.am,v
retrieving revision 1.1
diff -u -w -p -r1.1 Makefile.am
--- libparser/Makefile.am	1 Feb 2010 10:22:16 -0000	1.1
+++ libparser/Makefile.am	17 Mar 2010 06:51:29 -0000
@@ -14,7 +14,7 @@ noinst_LIBRARIES = libgloparser.a

 noinst_HEADERS = parser.h internal.h asm_parse.h

-libgloparser_a_SOURCES = parser.c C.c Cpp.c asm_parse.c asm_scan.c java.c php.c
+libgloparser_a_SOURCES = parser.c C.c Cpp.c asm_parse.c asm_scan.c
java.c php.c js.c

 INCLUDES = @INCLUDES@

Index: libparser/internal.h
===================================================================
RCS file: /sources/global/global/libparser/internal.h,v
retrieving revision 1.2
diff -u -w -p -r1.2 internal.h
--- libparser/internal.h	5 Feb 2010 00:50:09 -0000	1.2
+++ libparser/internal.h	17 Mar 2010 06:51:29 -0000
@@ -42,6 +42,7 @@ void Cpp(const struct parser_param *);
 void java(const struct parser_param *);
 void php(const struct parser_param *);
 void assembly(const struct parser_param *);
+void javaScript(const struct parser_param *);

 void dbg_print(int, const char *);

Index: libparser/parser.c
===================================================================
RCS file: /sources/global/global/libparser/parser.c,v
retrieving revision 1.2
diff -u -w -p -r1.2 parser.c
--- libparser/parser.c	5 Feb 2010 00:50:09 -0000	1.2
+++ libparser/parser.c	17 Mar 2010 06:51:29 -0000
@@ -178,7 +178,8 @@ static const struct lang_entry lang_swit
 	{"cpp",		Cpp},
 	{"java",	java},
 	{"php",		php},
-	{"asm",		assembly}
+	{"asm",		assembly},
+	{"js",    javaScript}
 };
 #define DEFAULT_ENTRY &lang_switch[0]
 /*
Index: gtags-parser/js_res.in
===================================================================
RCS file: gtags-parser/js_res.in
diff -N gtags-parser/js_res.in
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gtags-parser/js_res.in	17 Mar 2010 06:51:29 -0000
@@ -0,0 +1,74 @@
+;; Process this file with reserved.pl to create gperf source.
+;
+; Copyright (c) 2003 Tama Communications Corporation
+;
+; This file is free software; as a special exception the author gives
+; unlimited permission to copy and/or distribute it, with or without
+; modifications, as long as this notice is preserved.
+;
+; This program is distributed in the hope that it will be useful, but
+; WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
+; implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+;
+; JavaScript reserved word list.
+;
+abstract		word
+boolean 		word
+break 			word
+byte			word
+case 			word
+catch 			word
+char 			word
+class 			word
+const 			word
+continue		word
+debugger 		word
+default 		word
+delete 			word
+do 				word
+double			word
+else 			word
+enum 			word
+export 			word
+extends			word
+false 			word
+final 			word
+finally 		word
+float 			word
+for 			word
+function		word
+goto			word
+if 				word
+implements 		word
+import 			word
+in				word
+instanceof 		word
+int 			word
+interface		word
+long			word
+native 			word
+new 			word
+null			word
+package 		word
+private 		word
+protected		word
+public			word
+return			word
+short 			word
+static			word
+super 			word
+switch 			word
+synchronized	word
+this 			word
+throw			word
+throws 			word
+transient 		word
+true 			word
+try 			word
+typeof			word
+var 			word
+volatile 		word
+void			word
+while 			word
+with			word
Index: configure.ac
===================================================================
RCS file: /sources/global/global/configure.ac,v
retrieving revision 1.104
diff -u -w -p -r1.104 configure.ac
--- configure.ac	5 Feb 2010 00:55:43 -0000	1.104
+++ configure.ac	17 Mar 2010 06:51:29 -0000
@@ -194,7 +194,7 @@ dnl DEFAULTSKIP: You need not list files
 dnl		Such files are skipped even in case of not being in the list.
 dnl
 DEFAULTSKIP='GPATH,GTAGS,GRTAGS,GSYMS,HTML/,HTML.pub/,html/,tags,TAGS,ID,y.tab.c,y.tab.h,cscope.out,cscope.po.out,cscope.in.out,SCCS/,RCS/,CVS/,CVSROOT/,{arch}/,autom4te.cache/'
-DEFAULTLANGMAP='c:.c.h,yacc:.y,asm:.s.S,java:.java,cpp:.c++.cc.cpp.cxx.hxx.hpp.C.H,php:.php.php3.phtml'
+DEFAULTLANGMAP='c:.c.h,yacc:.y,asm:.s.S,java:.java,cpp:.c++.cc.cpp.cxx.hxx.hpp.C.H,php:.php.php3.phtml,js:.js.as.ks'
 AC_SUBST(DEFAULTSKIP)
 AC_SUBST(DEFAULTLANGMAP)

Index: reconf.sh
===================================================================
RCS file: /sources/global/global/reconf.sh,v
retrieving revision 1.37
diff -u -w -p -r1.37 reconf.sh
--- reconf.sh	1 Feb 2010 10:22:15 -0000	1.37
+++ reconf.sh	17 Mar 2010 06:51:29 -0000
@@ -63,7 +63,7 @@ done
 #
 echo "- Preparing parser source ..."
 (cd gtags-parser; set -x
-for lang in c cpp java php asm; do
+for lang in c cpp java php asm js; do
 	name=${lang}_res
 	perl ./reserved.pl --prefix=$lang ${lang}_res.in > ${name}.gpf
 	option=`perl ./reserved.pl --prefix=$lang --option`

-- 
/vg