aboutsummaryrefslogtreecommitdiff
path: root/js/codemirror/mode/sparql
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/codemirror/mode/sparql
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/codemirror/mode/sparql')
-rw-r--r--js/codemirror/mode/sparql/index.html41
-rw-r--r--js/codemirror/mode/sparql/sparql.js143
2 files changed, 184 insertions, 0 deletions
diff --git a/js/codemirror/mode/sparql/index.html b/js/codemirror/mode/sparql/index.html
new file mode 100644
index 00000000..a0e2f4e0
--- /dev/null
+++ b/js/codemirror/mode/sparql/index.html
@@ -0,0 +1,41 @@
1<!doctype html>
2<html>
3 <head>
4 <title>CodeMirror 2: SPARQL mode</title>
5 <link rel="stylesheet" href="../../lib/codemirror.css">
6 <script src="../../lib/codemirror.js"></script>
7 <script src="sparql.js"></script>
8 <link rel="stylesheet" href="../../theme/default.css">
9 <style>.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
10 <link rel="stylesheet" href="../../css/docs.css">
11 </head>
12 <body>
13 <h1>CodeMirror 2: SPARQL mode</h1>
14 <form><textarea id="code" name="code">
15PREFIX a: &lt;http://www.w3.org/2000/10/annotation-ns#>
16PREFIX dc: &lt;http://purl.org/dc/elements/1.1/>
17PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/>
18
19# Comment!
20
21SELECT ?given ?family
22WHERE {
23 ?annot a:annotates &lt;http://www.w3.org/TR/rdf-sparql-query/> .
24 ?annot dc:creator ?c .
25 OPTIONAL {?c foaf:given ?given ;
26 foaf:family ?family } .
27 FILTER isBlank(?c)
28}
29</textarea></form>
30 <script>
31 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
32 mode: "application/x-sparql-query",
33 tabMode: "indent",
34 matchBrackets: true
35 });
36 </script>
37
38 <p><strong>MIME types defined:</strong> <code>application/x-sparql-query</code>.</p>
39
40 </body>
41</html>
diff --git a/js/codemirror/mode/sparql/sparql.js b/js/codemirror/mode/sparql/sparql.js
new file mode 100644
index 00000000..ceb52942
--- /dev/null
+++ b/js/codemirror/mode/sparql/sparql.js
@@ -0,0 +1,143 @@
1CodeMirror.defineMode("sparql", function(config) {
2 var indentUnit = config.indentUnit;
3 var curPunc;
4
5 function wordRegexp(words) {
6 return new RegExp("^(?:" + words.join("|") + ")$", "i");
7 }
8 var ops = wordRegexp(["str", "lang", "langmatches", "datatype", "bound", "sameterm", "isiri", "isuri",
9 "isblank", "isliteral", "union", "a"]);
10 var keywords = wordRegexp(["base", "prefix", "select", "distinct", "reduced", "construct", "describe",
11 "ask", "from", "named", "where", "order", "limit", "offset", "filter", "optional",
12 "graph", "by", "asc", "desc"]);
13 var operatorChars = /[*+\-<>=&|]/;
14
15 function tokenBase(stream, state) {
16 var ch = stream.next();
17 curPunc = null;
18 if (ch == "$" || ch == "?") {
19 stream.match(/^[\w\d]*/);
20 return "variable-2";
21 }
22 else if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
23 stream.match(/^[^\s\u00a0>]*>?/);
24 return "atom";
25 }
26 else if (ch == "\"" || ch == "'") {
27 state.tokenize = tokenLiteral(ch);
28 return state.tokenize(stream, state);
29 }
30 else if (/[{}\(\),\.;\[\]]/.test(ch)) {
31 curPunc = ch;
32 return null;
33 }
34 else if (ch == "#") {
35 stream.skipToEnd();
36 return "comment";
37 }
38 else if (operatorChars.test(ch)) {
39 stream.eatWhile(operatorChars);
40 return null;
41 }
42 else if (ch == ":") {
43 stream.eatWhile(/[\w\d\._\-]/);
44 return "atom";
45 }
46 else {
47 stream.eatWhile(/[_\w\d]/);
48 if (stream.eat(":")) {
49 stream.eatWhile(/[\w\d_\-]/);
50 return "atom";
51 }
52 var word = stream.current(), type;
53 if (ops.test(word))
54 return null;
55 else if (keywords.test(word))
56 return "keyword";
57 else
58 return "variable";
59 }
60 }
61
62 function tokenLiteral(quote) {
63 return function(stream, state) {
64 var escaped = false, ch;
65 while ((ch = stream.next()) != null) {
66 if (ch == quote && !escaped) {
67 state.tokenize = tokenBase;
68 break;
69 }
70 escaped = !escaped && ch == "\\";
71 }
72 return "string";
73 };
74 }
75
76 function pushContext(state, type, col) {
77 state.context = {prev: state.context, indent: state.indent, col: col, type: type};
78 }
79 function popContext(state) {
80 state.indent = state.context.indent;
81 state.context = state.context.prev;
82 }
83
84 return {
85 startState: function(base) {
86 return {tokenize: tokenBase,
87 context: null,
88 indent: 0,
89 col: 0};
90 },
91
92 token: function(stream, state) {
93 if (stream.sol()) {
94 if (state.context && state.context.align == null) state.context.align = false;
95 state.indent = stream.indentation();
96 }
97 if (stream.eatSpace()) return null;
98 var style = state.tokenize(stream, state);
99
100 if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
101 state.context.align = true;
102 }
103
104 if (curPunc == "(") pushContext(state, ")", stream.column());
105 else if (curPunc == "[") pushContext(state, "]", stream.column());
106 else if (curPunc == "{") pushContext(state, "}", stream.column());
107 else if (/[\]\}\)]/.test(curPunc)) {
108 while (state.context && state.context.type == "pattern") popContext(state);
109 if (state.context && curPunc == state.context.type) popContext(state);
110 }
111 else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
112 else if (/atom|string|variable/.test(style) && state.context) {
113 if (/[\}\]]/.test(state.context.type))
114 pushContext(state, "pattern", stream.column());
115 else if (state.context.type == "pattern" && !state.context.align) {
116 state.context.align = true;
117 state.context.col = stream.column();
118 }
119 }
120
121 return style;
122 },
123
124 indent: function(state, textAfter) {
125 var firstChar = textAfter && textAfter.charAt(0);
126 var context = state.context;
127 if (/[\]\}]/.test(firstChar))
128 while (context && context.type == "pattern") context = context.prev;
129
130 var closing = context && firstChar == context.type;
131 if (!context)
132 return 0;
133 else if (context.type == "pattern")
134 return context.col;
135 else if (context.align)
136 return context.col + (closing ? 0 : 1);
137 else
138 return context.indent + (closing ? 0 : indentUnit);
139 }
140 };
141});
142
143CodeMirror.defineMIME("application/x-sparql-query", "sparql");