From 90a85d6be1242b1daaf625a16ae8d2b01fdb27ee Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Sat, 22 Oct 2011 02:08:48 -0300 Subject: [PATCH] Add support for Qt's QML language Reusing the JS parser, since QML is 'almost' JavaScript. The approximation is good enough for the line counting purposes. --- src/hash/extensions.gperf | 1 + src/hash/languages.gperf | 1 + src/hash/parsers.gperf | 1 + src/languages.h | 1 + src/parsers/javascript.rl | 12 ++++++++++++ test/detect_files/foo.qml | 7 +++++++ test/expected_dir/example.qml | 17 +++++++++++++++++ test/src_dir/example.qml | 17 +++++++++++++++++ test/unit/detector_test.h | 1 + test/unit/parser_test.h | 1 + test/unit/parsers/test_qml.h | 23 +++++++++++++++++++++++ 11 files changed, 82 insertions(+) create mode 100644 test/detect_files/foo.qml create mode 100644 test/expected_dir/example.qml create mode 100644 test/src_dir/example.qml create mode 100644 test/unit/parsers/test_qml.h diff --git a/src/hash/extensions.gperf b/src/hash/extensions.gperf index 97a1498..5ef510c 100755 --- a/src/hash/extensions.gperf +++ b/src/hash/extensions.gperf @@ -149,6 +149,7 @@ pp, DISAMBIGUATE("pp") ppt, BINARY pro, DISAMBIGUATE("pro") py, LANG_PYTHON +qml, LANG_QML qt, BINARY r, DISAMBIGUATE("r") r3, LANG_REBOL diff --git a/src/hash/languages.gperf b/src/hash/languages.gperf index dc7eeb4..aebf7a1 100755 --- a/src/hash/languages.gperf +++ b/src/hash/languages.gperf @@ -77,6 +77,7 @@ php, LANG_PHP, "PHP", 0 pike, LANG_PIKE, "Pike", 0 puppet, LANG_PUPPET, "Puppet", 0 python, LANG_PYTHON, "Python", 0 +qml, LANG_QML, "QML", 1 r, LANG_R, "R", 0 racket, LANG_RACKET, "Racket", 0 rebol, LANG_REBOL, "REBOL", 0 diff --git a/src/hash/parsers.gperf b/src/hash/parsers.gperf index 9c98093..10facd2 100644 --- a/src/hash/parsers.gperf +++ b/src/hash/parsers.gperf @@ -164,6 +164,7 @@ php, parse_phtml pike, parse_pike puppet, parse_puppet python, parse_python +qml, parse_qml r, parse_r racket, parse_racket rebol, parse_rebol diff --git a/src/languages.h b/src/languages.h index e62ca97..8a24d0d 100755 --- a/src/languages.h +++ b/src/languages.h @@ -79,6 +79,7 @@ #define LANG_PIKE "pike" #define LANG_PUPPET "puppet" #define LANG_PYTHON "python" +#define LANG_QML "qml" #define LANG_R "r" #define LANG_RACKET "racket" #define LANG_REBOL "rebol" diff --git a/src/parsers/javascript.rl b/src/parsers/javascript.rl index 1f34177..62dfc3e 100644 --- a/src/parsers/javascript.rl +++ b/src/parsers/javascript.rl @@ -134,4 +134,16 @@ void parse_javascript(char *buffer, int length, int count, if (count) { process_last_line(JS_LANG) } } +const char *QML_LANG = LANG_QML; +const char *ORIG_JS_LANG = LANG_JAVASCRIPT; +void parse_qml(char *buffer, int length, int count, + void (*callback) (const char *lang, const char *entity, + int s, int e, void *udata), + void *userdata + ) { + JS_LANG = QML_LANG; + parse_javascript(buffer, length, count, callback, userdata); + JS_LANG = ORIG_JS_LANG; +} + #endif diff --git a/test/detect_files/foo.qml b/test/detect_files/foo.qml new file mode 100644 index 0000000..e30475a --- /dev/null +++ b/test/detect_files/foo.qml @@ -0,0 +1,7 @@ +import QtQuick 2.0 + +Rectangle { + width: 200 + height: 200 + color: "crimson" +} diff --git a/test/expected_dir/example.qml b/test/expected_dir/example.qml new file mode 100644 index 0000000..ab96abc --- /dev/null +++ b/test/expected_dir/example.qml @@ -0,0 +1,17 @@ +qml comment // Just an example of QML file... +qml blank +qml code import QtQuick 2.0 +qml blank +qml code Rectangle { +qml code width: 200 +qml code height: 200 +qml code color: "crimson" +qml blank +qml code MouseArea { +qml code anchors.fill: parent +qml code onClicked: { +qml comment // Was clicked +qml code Qt.quit(); +qml code } +qml code } +qml code } diff --git a/test/src_dir/example.qml b/test/src_dir/example.qml new file mode 100644 index 0000000..b92a82d --- /dev/null +++ b/test/src_dir/example.qml @@ -0,0 +1,17 @@ +// Just an example of QML file... + +import QtQuick 2.0 + +Rectangle { + width: 200 + height: 200 + color: "crimson" + + MouseArea { + anchors.fill: parent + onClicked: { + // Was clicked + Qt.quit(); + } + } +} diff --git a/test/unit/detector_test.h b/test/unit/detector_test.h index a67c735..0f7035c 100755 --- a/test/unit/detector_test.h +++ b/test/unit/detector_test.h @@ -129,6 +129,7 @@ void test_detector_detect_polyglot() { ASSERT_DETECT(LANG_NSIS, "foo.nsi"); ASSERT_DETECT(LANG_NSIS, "foo.nsh"); ASSERT_DETECT(LANG_COFFEESCRIPT, "foo.coffee"); + ASSERT_DETECT(LANG_QML, "foo.qml"); ASSERT_NODETECT("empty.inc"); } diff --git a/test/unit/parser_test.h b/test/unit/parser_test.h index 28a3c9a..a04338a 100644 --- a/test/unit/parser_test.h +++ b/test/unit/parser_test.h @@ -133,6 +133,7 @@ void test_parser_verify_entity(SourceFile *sf, const char *entity, #include "parsers/test_pike.h" #include "parsers/test_puppet.h" #include "parsers/test_python.h" +#include "parsers/test_qml.h" #include "parsers/test_r.h" #include "parsers/test_racket.h" #include "parsers/test_rebol.h" diff --git a/test/unit/parsers/test_qml.h b/test/unit/parsers/test_qml.h new file mode 100644 index 0000000..d4ba843 --- /dev/null +++ b/test/unit/parsers/test_qml.h @@ -0,0 +1,23 @@ + +void test_qml_comments() { + test_parser_verify_parse( + test_parser_sourcefile("qml", " //comment"), + "qml", "", "//comment", 0 + ); +} + +void test_qml_comment_entities() { + test_parser_verify_entity( + test_parser_sourcefile("qml", " //comment"), + "comment", "//comment" + ); + test_parser_verify_entity( + test_parser_sourcefile("qml", " /*comment*/"), + "comment", "/*comment*/" + ); +} + +void all_qml_tests() { + test_qml_comments(); + test_qml_comment_entities(); +} -- 2.32.0.93.g670b81a890