aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/mode/ntriples/ntriples.js
diff options
context:
space:
mode:
Diffstat (limited to 'imports/codemirror/mode/ntriples/ntriples.js')
-rwxr-xr-ximports/codemirror/mode/ntriples/ntriples.js172
1 files changed, 172 insertions, 0 deletions
diff --git a/imports/codemirror/mode/ntriples/ntriples.js b/imports/codemirror/mode/ntriples/ntriples.js
new file mode 100755
index 00000000..3b6cb416
--- /dev/null
+++ b/imports/codemirror/mode/ntriples/ntriples.js
@@ -0,0 +1,172 @@
1/**********************************************************
2* This script provides syntax highlighting support for
3* the Ntriples format.
4* Ntriples format specification:
5* http://www.w3.org/TR/rdf-testcases/#ntriples
6***********************************************************/
7
8/*
9 The following expression defines the defined ASF grammar transitions.
10
11 pre_subject ->
12 {
13 ( writing_subject_uri | writing_bnode_uri )
14 -> pre_predicate
15 -> writing_predicate_uri
16 -> pre_object
17 -> writing_object_uri | writing_object_bnode |
18 (
19 writing_object_literal
20 -> writing_literal_lang | writing_literal_type
21 )
22 -> post_object
23 -> BEGIN
24 } otherwise {
25 -> ERROR
26 }
27*/
28CodeMirror.defineMode("ntriples", function() {
29
30 Location = {
31 PRE_SUBJECT : 0,
32 WRITING_SUB_URI : 1,
33 WRITING_BNODE_URI : 2,
34 PRE_PRED : 3,
35 WRITING_PRED_URI : 4,
36 PRE_OBJ : 5,
37 WRITING_OBJ_URI : 6,
38 WRITING_OBJ_BNODE : 7,
39 WRITING_OBJ_LITERAL : 8,
40 WRITING_LIT_LANG : 9,
41 WRITING_LIT_TYPE : 10,
42 POST_OBJ : 11,
43 ERROR : 12
44 };
45 function transitState(currState, c) {
46 var currLocation = currState.location;
47 var ret;
48
49 // Opening.
50 if (currLocation == Location.PRE_SUBJECT && c == '<') ret = Location.WRITING_SUB_URI;
51 else if(currLocation == Location.PRE_SUBJECT && c == '_') ret = Location.WRITING_BNODE_URI;
52 else if(currLocation == Location.PRE_PRED && c == '<') ret = Location.WRITING_PRED_URI;
53 else if(currLocation == Location.PRE_OBJ && c == '<') ret = Location.WRITING_OBJ_URI;
54 else if(currLocation == Location.PRE_OBJ && c == '_') ret = Location.WRITING_OBJ_BNODE;
55 else if(currLocation == Location.PRE_OBJ && c == '"') ret = Location.WRITING_OBJ_LITERAL;
56
57 // Closing.
58 else if(currLocation == Location.WRITING_SUB_URI && c == '>') ret = Location.PRE_PRED;
59 else if(currLocation == Location.WRITING_BNODE_URI && c == ' ') ret = Location.PRE_PRED;
60 else if(currLocation == Location.WRITING_PRED_URI && c == '>') ret = Location.PRE_OBJ;
61 else if(currLocation == Location.WRITING_OBJ_URI && c == '>') ret = Location.POST_OBJ;
62 else if(currLocation == Location.WRITING_OBJ_BNODE && c == ' ') ret = Location.POST_OBJ;
63 else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '"') ret = Location.POST_OBJ;
64 else if(currLocation == Location.WRITING_LIT_LANG && c == ' ') ret = Location.POST_OBJ;
65 else if(currLocation == Location.WRITING_LIT_TYPE && c == '>') ret = Location.POST_OBJ;
66
67 // Closing typed and language literal.
68 else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '@') ret = Location.WRITING_LIT_LANG;
69 else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '^') ret = Location.WRITING_LIT_TYPE;
70
71 // Spaces.
72 else if( c == ' ' &&
73 (
74 currLocation == Location.PRE_SUBJECT ||
75 currLocation == Location.PRE_PRED ||
76 currLocation == Location.PRE_OBJ ||
77 currLocation == Location.POST_OBJ
78 )
79 ) ret = currLocation;
80
81 // Reset.
82 else if(currLocation == Location.POST_OBJ && c == '.') ret = Location.PRE_SUBJECT;
83
84 // Error
85 else ret = Location.ERROR;
86
87 currState.location=ret;
88 }
89
90 untilSpace = function(c) { return c != ' '; };
91 untilEndURI = function(c) { return c != '>'; };
92 return {
93 startState: function() {
94 return {
95 location : Location.PRE_SUBJECT,
96 uris : [],
97 anchors : [],
98 bnodes : [],
99 langs : [],
100 types : []
101 };
102 },
103 token: function(stream, state) {
104 var ch = stream.next();
105 if(ch == '<') {
106 transitState(state, ch);
107 var parsedURI = '';
108 stream.eatWhile( function(c) { if( c != '#' && c != '>' ) { parsedURI += c; return true; } return false;} );
109 state.uris.push(parsedURI);
110 if( stream.match('#', false) ) return 'variable';
111 stream.next();
112 transitState(state, '>');
113 return 'variable';
114 }
115 if(ch == '#') {
116 var parsedAnchor = '';
117 stream.eatWhile(function(c) { if(c != '>' && c != ' ') { parsedAnchor+= c; return true; } return false});
118 state.anchors.push(parsedAnchor);
119 return 'variable-2';
120 }
121 if(ch == '>') {
122 transitState(state, '>');
123 return 'variable';
124 }
125 if(ch == '_') {
126 transitState(state, ch);
127 var parsedBNode = '';
128 stream.eatWhile(function(c) { if( c != ' ' ) { parsedBNode += c; return true; } return false;});
129 state.bnodes.push(parsedBNode);
130 stream.next();
131 transitState(state, ' ');
132 return 'builtin';
133 }
134 if(ch == '"') {
135 transitState(state, ch);
136 stream.eatWhile( function(c) { return c != '"'; } );
137 stream.next();
138 if( stream.peek() != '@' && stream.peek() != '^' ) {
139 transitState(state, '"');
140 }
141 return 'string';
142 }
143 if( ch == '@' ) {
144 transitState(state, '@');
145 var parsedLang = '';
146 stream.eatWhile(function(c) { if( c != ' ' ) { parsedLang += c; return true; } return false;});
147 state.langs.push(parsedLang);
148 stream.next();
149 transitState(state, ' ');
150 return 'string-2';
151 }
152 if( ch == '^' ) {
153 stream.next();
154 transitState(state, '^');
155 var parsedType = '';
156 stream.eatWhile(function(c) { if( c != '>' ) { parsedType += c; return true; } return false;} );
157 state.types.push(parsedType);
158 stream.next();
159 transitState(state, '>');
160 return 'variable';
161 }
162 if( ch == ' ' ) {
163 transitState(state, ch);
164 }
165 if( ch == '.' ) {
166 transitState(state, ch);
167 }
168 }
169 };
170});
171
172CodeMirror.defineMIME("text/n-triples", "ntriples");