Debian Codegraph @

share/www/js/parsesparql.js

raw
1 var SparqlParser = Editor.Parser = (function() {
2 function wordRegexp(words) {
3 return new RegExp("^(?:" + words.join("|") + ")$", "i");
4 }
5 var ops = wordRegexp(["str", "lang", "langmatches", "datatype", "bound", "sameterm", "isiri", "isuri",
6 "isblank", "isliteral", "union", "a",
7 // SPARQL 1.1
8 "abs", "ceil", "floor", "round", "concat", "substring", "strlen", "ucase",
9 "lcase", "encode", "contains", "starts", "ends", "rand", "strdt", "strlang",
10 "bnode", "iri", "uri", "datatype", "regex", "isnumeric", "if", "coalesce",
11 "count", "sum", "min", "max", "avg", "group_concat", "sample"
12 ]);
13 var keywords = wordRegexp(["base", "prefix", "select", "distinct", "reduced", "construct", "describe",
14 "ask", "from", "named", "where", "order", "limit", "offset", "filter", "optional",
15 "graph", "by", "asc", "desc",
16 // SPARQL 1.1
17 "group",
18 "insert", "delete", "data", "load", "clear", "drop", "graph", "default",
19 "using", "with"]);
20 var operatorChars = /[*+\-<>=&|]/;
21
22 var tokenizeSparql = (function() {
23 function normal(source, setState) {
24 var ch = source.next();
25 if (ch == "$" || ch == "?") {
26 source.nextWhileMatches(/[\w\d]/);
27 return "sp-var";
28 }
29 else if (ch == "<" && !source.matches(/[\s\u00a0=]/)) {
30 source.nextWhileMatches(/[^\s\u00a0>]/);
31 if (source.equals(">")) source.next();
32 return "sp-uri";
33 }
34 else if (ch == "\"" || ch == "'") {
35 setState(inLiteral(ch));
36 return null;
37 }
38 else if (/[{}\(\),\.;\[\]]/.test(ch)) {
39 return "sp-punc";
40 }
41 else if (ch == "#") {
42 while (!source.endOfLine()) source.next();
43 return "sp-comment";
44 }
45 else if (operatorChars.test(ch)) {
46 source.nextWhileMatches(operatorChars);
47 return "sp-operator";
48 }
49 else if (ch == ":") {
50 source.nextWhileMatches(/[\w\d\._\-]/);
51 return "sp-prefixed";
52 }
53 else {
54 source.nextWhileMatches(/[_\w\d]/);
55 if (source.equals(":")) {
56 source.next();
57 source.nextWhileMatches(/[\w\d_\-]/);
58 return "sp-prefixed";
59 }
60 var word = source.get(), type;
61 if (ops.test(word))
62 type = "sp-operator";
63 else if (keywords.test(word))
64 type = "sp-keyword";
65 else
66 type = "sp-word";
67 return {style: type, content: word};
68 }
69 }
70
71 function inLiteral(quote) {
72 return function(source, setState) {
73 var escaped = false;
74 while (!source.endOfLine()) {
75 var ch = source.next();
76 if (ch == quote && !escaped) {
77 setState(normal);
78 break;
79 }
80 escaped = !escaped && ch == "\\";
81 }
82 return "sp-literal";
83 };
84 }
85
86 return function(source, startState) {
87 return tokenizer(source, startState || normal);
88 };
89 })();
90
91 function indentSparql(context) {
92 return function(nextChars) {
93 var firstChar = nextChars && nextChars.charAt(0);
94 if (/[\]\}]/.test(firstChar))
95 while (context && context.type == "pattern") context = context.prev;
96
97 var closing = context && firstChar == matching[context.type];
98 if (!context)
99 return 0;
100 else if (context.type == "pattern")
101 return context.col;
102 else if (context.align)
103 return context.col - (closing ? context.width : 0);
104 else
105 return context.indent + (closing ? 0 : indentUnit);
106 }
107 }
108
109 function parseSparql(source) {
110 var tokens = tokenizeSparql(source);
111 var context = null, indent = 0, col = 0;
112 function pushContext(type, width) {
113 context = {prev: context, indent: indent, col: col, type: type, width: width};
114 }
115 function popContext() {
116 context = context.prev;
117 }
118
119 var iter = {
120 next: function() {
121 var token = tokens.next(), type = token.style, content = token.content, width = token.value.length;
122
123 if (content == "\n") {
124 token.indentation = indentSparql(context);
125 indent = col = 0;
126 if (context && context.align == null) context.align = false;
127 }
128 else if (type == "whitespace" && col == 0) {
129 indent = width;
130 }
131 else if (type != "sp-comment" && context && context.align == null) {
132 context.align = true;
133 }
134
135 if (content != "\n") col += width;
136
137 if (/[\[\{\(]/.test(content)) {
138 pushContext(content, width);
139 }
140 else if (/[\]\}\)]/.test(content)) {
141 while (context && context.type == "pattern")
142 popContext();
143 if (context && content == matching[context.type])
144 popContext();
145 }
146 else if (content == "." && context && context.type == "pattern") {
147 popContext();
148 }
149 else if ((type == "sp-word" || type == "sp-prefixed" || type == "sp-uri" || type == "sp-var" || type == "sp-literal") &&
150 context && /[\{\[]/.test(context.type)) {
151 pushContext("pattern", width);
152 }
153
154 return token;
155 },
156
157 copy: function() {
158 var _context = context, _indent = indent, _col = col, _tokenState = tokens.state;
159 return function(source) {
160 tokens = tokenizeSparql(source, _tokenState);
161 context = _context;
162 indent = _indent;
163 col = _col;
164 return iter;
165 };
166 }
167 };
168 return iter;
169 }
170
171 return {make: parseSparql, electricChars: "}]"};
172 })();