1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | (function(mod) {
|
33 | if (typeof exports == "object" && typeof module == "object")
|
34 | mod(require("../../lib/codemirror"));
|
35 | else if (typeof define == "function" && define.amd)
|
36 | define(["../../lib/codemirror"], mod);
|
37 | else
|
38 | mod(CodeMirror);
|
39 | })(function(CodeMirror) {
|
40 | "use strict";
|
41 |
|
42 | CodeMirror.defineMode("ntriples", function() {
|
43 |
|
44 | var Location = {
|
45 | PRE_SUBJECT : 0,
|
46 | WRITING_SUB_URI : 1,
|
47 | WRITING_BNODE_URI : 2,
|
48 | PRE_PRED : 3,
|
49 | WRITING_PRED_URI : 4,
|
50 | PRE_OBJ : 5,
|
51 | WRITING_OBJ_URI : 6,
|
52 | WRITING_OBJ_BNODE : 7,
|
53 | WRITING_OBJ_LITERAL : 8,
|
54 | WRITING_LIT_LANG : 9,
|
55 | WRITING_LIT_TYPE : 10,
|
56 | POST_OBJ : 11,
|
57 | ERROR : 12
|
58 | };
|
59 | function transitState(currState, c) {
|
60 | var currLocation = currState.location;
|
61 | var ret;
|
62 |
|
63 |
|
64 | if (currLocation == Location.PRE_SUBJECT && c == '<') ret = Location.WRITING_SUB_URI;
|
65 | else if(currLocation == Location.PRE_SUBJECT && c == '_') ret = Location.WRITING_BNODE_URI;
|
66 | else if(currLocation == Location.PRE_PRED && c == '<') ret = Location.WRITING_PRED_URI;
|
67 | else if(currLocation == Location.PRE_OBJ && c == '<') ret = Location.WRITING_OBJ_URI;
|
68 | else if(currLocation == Location.PRE_OBJ && c == '_') ret = Location.WRITING_OBJ_BNODE;
|
69 | else if(currLocation == Location.PRE_OBJ && c == '"') ret = Location.WRITING_OBJ_LITERAL;
|
70 |
|
71 |
|
72 | else if(currLocation == Location.WRITING_SUB_URI && c == '>') ret = Location.PRE_PRED;
|
73 | else if(currLocation == Location.WRITING_BNODE_URI && c == ' ') ret = Location.PRE_PRED;
|
74 | else if(currLocation == Location.WRITING_PRED_URI && c == '>') ret = Location.PRE_OBJ;
|
75 | else if(currLocation == Location.WRITING_OBJ_URI && c == '>') ret = Location.POST_OBJ;
|
76 | else if(currLocation == Location.WRITING_OBJ_BNODE && c == ' ') ret = Location.POST_OBJ;
|
77 | else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '"') ret = Location.POST_OBJ;
|
78 | else if(currLocation == Location.WRITING_LIT_LANG && c == ' ') ret = Location.POST_OBJ;
|
79 | else if(currLocation == Location.WRITING_LIT_TYPE && c == '>') ret = Location.POST_OBJ;
|
80 |
|
81 |
|
82 | else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '@') ret = Location.WRITING_LIT_LANG;
|
83 | else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '^') ret = Location.WRITING_LIT_TYPE;
|
84 |
|
85 |
|
86 | else if( c == ' ' &&
|
87 | (
|
88 | currLocation == Location.PRE_SUBJECT ||
|
89 | currLocation == Location.PRE_PRED ||
|
90 | currLocation == Location.PRE_OBJ ||
|
91 | currLocation == Location.POST_OBJ
|
92 | )
|
93 | ) ret = currLocation;
|
94 |
|
95 |
|
96 | else if(currLocation == Location.POST_OBJ && c == '.') ret = Location.PRE_SUBJECT;
|
97 |
|
98 |
|
99 | else ret = Location.ERROR;
|
100 |
|
101 | currState.location=ret;
|
102 | }
|
103 |
|
104 | return {
|
105 | startState: function() {
|
106 | return {
|
107 | location : Location.PRE_SUBJECT,
|
108 | uris : [],
|
109 | anchors : [],
|
110 | bnodes : [],
|
111 | langs : [],
|
112 | types : []
|
113 | };
|
114 | },
|
115 | token: function(stream, state) {
|
116 | var ch = stream.next();
|
117 | if(ch == '<') {
|
118 | transitState(state, ch);
|
119 | var parsedURI = '';
|
120 | stream.eatWhile( function(c) { if( c != '#' && c != '>' ) { parsedURI += c; return true; } return false;} );
|
121 | state.uris.push(parsedURI);
|
122 | if( stream.match('#', false) ) return 'variable';
|
123 | stream.next();
|
124 | transitState(state, '>');
|
125 | return 'variable';
|
126 | }
|
127 | if(ch == '#') {
|
128 | var parsedAnchor = '';
|
129 | stream.eatWhile(function(c) { if(c != '>' && c != ' ') { parsedAnchor+= c; return true; } return false;});
|
130 | state.anchors.push(parsedAnchor);
|
131 | return 'variable-2';
|
132 | }
|
133 | if(ch == '>') {
|
134 | transitState(state, '>');
|
135 | return 'variable';
|
136 | }
|
137 | if(ch == '_') {
|
138 | transitState(state, ch);
|
139 | var parsedBNode = '';
|
140 | stream.eatWhile(function(c) { if( c != ' ' ) { parsedBNode += c; return true; } return false;});
|
141 | state.bnodes.push(parsedBNode);
|
142 | stream.next();
|
143 | transitState(state, ' ');
|
144 | return 'builtin';
|
145 | }
|
146 | if(ch == '"') {
|
147 | transitState(state, ch);
|
148 | stream.eatWhile( function(c) { return c != '"'; } );
|
149 | stream.next();
|
150 | if( stream.peek() != '@' && stream.peek() != '^' ) {
|
151 | transitState(state, '"');
|
152 | }
|
153 | return 'string';
|
154 | }
|
155 | if( ch == '@' ) {
|
156 | transitState(state, '@');
|
157 | var parsedLang = '';
|
158 | stream.eatWhile(function(c) { if( c != ' ' ) { parsedLang += c; return true; } return false;});
|
159 | state.langs.push(parsedLang);
|
160 | stream.next();
|
161 | transitState(state, ' ');
|
162 | return 'string-2';
|
163 | }
|
164 | if( ch == '^' ) {
|
165 | stream.next();
|
166 | transitState(state, '^');
|
167 | var parsedType = '';
|
168 | stream.eatWhile(function(c) { if( c != '>' ) { parsedType += c; return true; } return false;} );
|
169 | state.types.push(parsedType);
|
170 | stream.next();
|
171 | transitState(state, '>');
|
172 | return 'variable';
|
173 | }
|
174 | if( ch == ' ' ) {
|
175 | transitState(state, ch);
|
176 | }
|
177 | if( ch == '.' ) {
|
178 | transitState(state, ch);
|
179 | }
|
180 | }
|
181 | };
|
182 | });
|
183 |
|
184 |
|
185 |
|
186 | CodeMirror.defineMIME("application/n-triples", "ntriples");
|
187 |
|
188 |
|
189 |
|
190 | CodeMirror.defineMIME("application/n-quads", "ntriples");
|
191 |
|
192 |
|
193 | CodeMirror.defineMIME("text/n-triples", "ntriples");
|
194 |
|
195 | });
|