From e07c11f643454c1c9f84586502acd0e79bd50904 Mon Sep 17 00:00:00 2001 From: maciek Date: Wed, 30 Jan 2008 22:22:18 +0100 Subject: [PATCH] Added support for TeX --- ext/ohcount_native/generator.rb | 4 ++- ext/ohcount_native/polyglots.c | 50 +++++++++++++++++++++++++++ lib/ohcount/detector.rb | 3 +- lib/ohcount/sloc_info.rb | 3 +- test/detect_files/foo.tex | 8 +++++ test/expected_dir/foo.tex/tex/blanks | 1 + test/expected_dir/foo.tex/tex/code | 3 ++ test/expected_dir/foo.tex/tex/comment | 2 ++ test/src_dir/foo.tex | 8 +++++ test/unit/detector_test.rb | 1 + test/unit/tex_test.rb | 13 +++++++ 11 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 test/detect_files/foo.tex create mode 100644 test/expected_dir/foo.tex/tex/blanks create mode 100644 test/expected_dir/foo.tex/tex/code create mode 100644 test/expected_dir/foo.tex/tex/comment create mode 100644 test/src_dir/foo.tex create mode 100644 test/unit/tex_test.rb diff --git a/ext/ohcount_native/generator.rb b/ext/ohcount_native/generator.rb index c872f13..188acf3 100644 --- a/ext/ohcount_native/generator.rb +++ b/ext/ohcount_native/generator.rb @@ -59,6 +59,7 @@ module Ohcount groovy = CMonoglot.new("groovy", '//', [e('/*'), e('*/')], true, false) clearsilver_template = ClearsilverTemplate.new("clearsilver_template", html, clearsilver) dmd = DMonoglot.new('dmd') + tex = CMonoglot.new("tex", '%', nil, false, false) polyglots = [ ada , @@ -101,7 +102,8 @@ module Ohcount php, rhtml, jsp, - clearsilver_template + clearsilver_template, + tex ] File.open("polyglots.c", "w") do |io| diff --git a/ext/ohcount_native/polyglots.c b/ext/ohcount_native/polyglots.c index 978bf98..516911d 100644 --- a/ext/ohcount_native/polyglots.c +++ b/ext/ohcount_native/polyglots.c @@ -398,6 +398,33 @@ Polyglot EMACSLISP_POLYGLOT = { NULL }; +/***************************************************************************** + haskell +*****************************************************************************/ +/* States */ +State HASKELL_CODE = { "HASKELL_CODE", "haskell", semantic_code }; +State HASKELL_DQUOTE_STRING = { "HASKELL_DQUOTE_STRING", "haskell", semantic_code }; +State HASKELL_SQUOTE_STRING = { "HASKELL_SQUOTE_STRING", "haskell", semantic_code }; +State HASKELL_LINE_COMMENT = { "HASKELL_LINE_COMMENT", "haskell", semantic_comment }; +State HASKELL_BLOCK_COMMENT = { "HASKELL_BLOCK_COMMENT", "haskell", semantic_comment }; +State *HASKELL_STATES[] = { &HASKELL_CODE, &HASKELL_DQUOTE_STRING, &HASKELL_SQUOTE_STRING, &HASKELL_LINE_COMMENT, &HASKELL_BLOCK_COMMENT, NULL }; +/* Transitions */ +Transition HASKELL_CODE__LINE_COMMENT_0 = { "--", &HASKELL_CODE, &HASKELL_LINE_COMMENT, ToEatsToken, false }; +Transition HASKELL_LINE_COMMENT__RETURN = { "\n", &HASKELL_LINE_COMMENT, RETURN, FromEatsToken, false }; +Transition HASKELL_CODE__BLOCK_COMMENT_0 = { "{\\-", &HASKELL_CODE, &HASKELL_BLOCK_COMMENT, ToEatsToken, false }; +Transition HASKELL_BLOCK_COMMENT__RETURN_0 = { "\\-}", &HASKELL_BLOCK_COMMENT, RETURN, FromEatsToken, false }; +Transition HASKELL_CODE__DQUOTE_STRING = { "\"", &HASKELL_CODE, &HASKELL_DQUOTE_STRING, ToEatsToken, false }; +Transition HASKELL_DQUOTE_STRING__DQUOTE_STRING_ESC_SLASH = { "\\\\\\\\", &HASKELL_DQUOTE_STRING, &HASKELL_DQUOTE_STRING, ToEatsToken, true }; +Transition HASKELL_DQUOTE_STRING__DQUOTE_STRING_ESC = { "\\\\\"", &HASKELL_DQUOTE_STRING, &HASKELL_DQUOTE_STRING, ToEatsToken, true }; +Transition HASKELL_DQUOTE_STRING__RETURN = { "\"", &HASKELL_DQUOTE_STRING, RETURN, FromEatsToken, false }; +Transition *HASKELL_TRANSITIONS[] = { &HASKELL_CODE__LINE_COMMENT_0, &HASKELL_LINE_COMMENT__RETURN, &HASKELL_CODE__BLOCK_COMMENT_0, &HASKELL_BLOCK_COMMENT__RETURN_0, &HASKELL_CODE__DQUOTE_STRING, &HASKELL_DQUOTE_STRING__DQUOTE_STRING_ESC_SLASH, &HASKELL_DQUOTE_STRING__DQUOTE_STRING_ESC, &HASKELL_DQUOTE_STRING__RETURN, NULL}; +Polyglot HASKELL_POLYGLOT = { + "haskell", + HASKELL_STATES, + HASKELL_TRANSITIONS, + NULL +}; + /***************************************************************************** lisp *****************************************************************************/ @@ -1004,6 +1031,27 @@ Polyglot CLEARSILVER_TEMPLATE_POLYGLOT = { NULL }; +/***************************************************************************** + tex +*****************************************************************************/ +/* States */ +State TEX_CODE = { "TEX_CODE", "tex", semantic_code }; +State TEX_DQUOTE_STRING = { "TEX_DQUOTE_STRING", "tex", semantic_code }; +State TEX_SQUOTE_STRING = { "TEX_SQUOTE_STRING", "tex", semantic_code }; +State TEX_LINE_COMMENT = { "TEX_LINE_COMMENT", "tex", semantic_comment }; +State TEX_BLOCK_COMMENT = { "TEX_BLOCK_COMMENT", "tex", semantic_comment }; +State *TEX_STATES[] = { &TEX_CODE, &TEX_DQUOTE_STRING, &TEX_SQUOTE_STRING, &TEX_LINE_COMMENT, &TEX_BLOCK_COMMENT, NULL }; +/* Transitions */ +Transition TEX_CODE__LINE_COMMENT_0 = { "%", &TEX_CODE, &TEX_LINE_COMMENT, ToEatsToken, false }; +Transition TEX_LINE_COMMENT__RETURN = { "\n", &TEX_LINE_COMMENT, RETURN, FromEatsToken, false }; +Transition *TEX_TRANSITIONS[] = { &TEX_CODE__LINE_COMMENT_0, &TEX_LINE_COMMENT__RETURN, NULL}; +Polyglot TEX_POLYGLOT = { + "tex", + TEX_STATES, + TEX_TRANSITIONS, + NULL +}; + /***************************************************************************** POLYGLOTS @@ -1024,6 +1072,7 @@ Polyglot *POLYGLOTS[] = { &JAVA_POLYGLOT, &JAVASCRIPT_POLYGLOT, &EMACSLISP_POLYGLOT, + &HASKELL_POLYGLOT, &LISP_POLYGLOT, &LUA_POLYGLOT, &MATLAB_POLYGLOT, @@ -1047,5 +1096,6 @@ Polyglot *POLYGLOTS[] = { &RHTML_POLYGLOT, &JSP_POLYGLOT, &CLEARSILVER_TEMPLATE_POLYGLOT, + &TEX_POLYGLOT, NULL }; diff --git a/lib/ohcount/detector.rb b/lib/ohcount/detector.rb index 38d4aac..3300d17 100644 --- a/lib/ohcount/detector.rb +++ b/lib/ohcount/detector.rb @@ -164,7 +164,8 @@ class Ohcount::Detector '.vbs' => "visualbasic", '.xml' => "xml", '.d' => 'dmd', - '.di' => 'dmd' + '.di' => 'dmd', + '.tex' => 'tex' } protected diff --git a/lib/ohcount/sloc_info.rb b/lib/ohcount/sloc_info.rb index b63038a..c1d34a0 100644 --- a/lib/ohcount/sloc_info.rb +++ b/lib/ohcount/sloc_info.rb @@ -67,7 +67,8 @@ class Ohcount::SlocInfo 'vala' => {:nice_name => 'Vala' , :category => 0}, 'visualbasic' => {:nice_name => 'Visual Basic' , :category => 0}, 'xml' => {:nice_name => 'XML' , :category => 1}, - 'dmd' => {:nice_name => 'D' , :category => 0} + 'dmd' => {:nice_name => 'D' , :category => 0}, + 'tex' => {:nice_name => 'TeX/LaTeX' , :category => 1} } def language_nice_name diff --git a/test/detect_files/foo.tex b/test/detect_files/foo.tex new file mode 100644 index 0000000..a0d4a5e --- /dev/null +++ b/test/detect_files/foo.tex @@ -0,0 +1,8 @@ +\begin{document} +\texbf Hello world + + +% ... +% sample comment + +\end{document} diff --git a/test/expected_dir/foo.tex/tex/blanks b/test/expected_dir/foo.tex/tex/blanks new file mode 100644 index 0000000..e440e5c --- /dev/null +++ b/test/expected_dir/foo.tex/tex/blanks @@ -0,0 +1 @@ +3 \ No newline at end of file diff --git a/test/expected_dir/foo.tex/tex/code b/test/expected_dir/foo.tex/tex/code new file mode 100644 index 0000000..aec1334 --- /dev/null +++ b/test/expected_dir/foo.tex/tex/code @@ -0,0 +1,3 @@ +\begin{document} +\texbf Hello world +\end{document} diff --git a/test/expected_dir/foo.tex/tex/comment b/test/expected_dir/foo.tex/tex/comment new file mode 100644 index 0000000..1a8caa7 --- /dev/null +++ b/test/expected_dir/foo.tex/tex/comment @@ -0,0 +1,2 @@ +% +% sample comment diff --git a/test/src_dir/foo.tex b/test/src_dir/foo.tex new file mode 100644 index 0000000..a54a999 --- /dev/null +++ b/test/src_dir/foo.tex @@ -0,0 +1,8 @@ +\begin{document} +\texbf Hello world + + +% +% sample comment + +\end{document} diff --git a/test/unit/detector_test.rb b/test/unit/detector_test.rb index ebddb03..e3eccb8 100644 --- a/test/unit/detector_test.rb +++ b/test/unit/detector_test.rb @@ -39,6 +39,7 @@ class Ohcount::DetectorTest < Ohcount::Test assert_equal "objective_c", do_detect("foo_objective_c.h", ["foo_objective_c.h, different_than_foo.m"]) assert_equal "php", do_detect("upper_case_php") assert_equal "vala", do_detect("foo.vala") + assert_equal "tex", do_detect("foo.tex") assert_equal "lisp", do_detect("core.lisp") assert_equal "dmd", do_detect("foo.d") end diff --git a/test/unit/tex_test.rb b/test/unit/tex_test.rb new file mode 100644 index 0000000..15ac70f --- /dev/null +++ b/test/unit/tex_test.rb @@ -0,0 +1,13 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class Ohcount::TeXTest < Ohcount::Test + + def test_comment + lb = [Ohcount::LanguageBreakdown.new("tex", "", "%comment", 0)] + assert_equal lb, Ohcount::parse(" %comment", "tex") + end + + def test_comprehensive + verify_parse("foo.tex") + end +end -- 2.32.0.93.g670b81a890