ntriples.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. /**********************************************************
  4. * This script provides syntax highlighting support for
  5. * the Ntriples format.
  6. * Ntriples format specification:
  7. * http://www.w3.org/TR/rdf-testcases/#ntriples
  8. ***********************************************************/
  9. /*
  10. The following expression defines the defined ASF grammar transitions.
  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. */
  28. (function(mod) {
  29. if (typeof exports == "object" && typeof module == "object") // CommonJS
  30. mod(require("../../lib/codemirror"));
  31. else if (typeof define == "function" && define.amd) // AMD
  32. define(["../../lib/codemirror"], mod);
  33. else // Plain browser env
  34. mod(CodeMirror);
  35. })(function(CodeMirror) {
  36. "use strict";
  37. CodeMirror.defineMode("ntriples", function() {
  38. var Location = {
  39. PRE_SUBJECT : 0,
  40. WRITING_SUB_URI : 1,
  41. WRITING_BNODE_URI : 2,
  42. PRE_PRED : 3,
  43. WRITING_PRED_URI : 4,
  44. PRE_OBJ : 5,
  45. WRITING_OBJ_URI : 6,
  46. WRITING_OBJ_BNODE : 7,
  47. WRITING_OBJ_LITERAL : 8,
  48. WRITING_LIT_LANG : 9,
  49. WRITING_LIT_TYPE : 10,
  50. POST_OBJ : 11,
  51. ERROR : 12
  52. };
  53. function transitState(currState, c) {
  54. var currLocation = currState.location;
  55. var ret;
  56. // Opening.
  57. if (currLocation == Location.PRE_SUBJECT && c == '<') ret = Location.WRITING_SUB_URI;
  58. else if(currLocation == Location.PRE_SUBJECT && c == '_') ret = Location.WRITING_BNODE_URI;
  59. else if(currLocation == Location.PRE_PRED && c == '<') ret = Location.WRITING_PRED_URI;
  60. else if(currLocation == Location.PRE_OBJ && c == '<') ret = Location.WRITING_OBJ_URI;
  61. else if(currLocation == Location.PRE_OBJ && c == '_') ret = Location.WRITING_OBJ_BNODE;
  62. else if(currLocation == Location.PRE_OBJ && c == '"') ret = Location.WRITING_OBJ_LITERAL;
  63. // Closing.
  64. else if(currLocation == Location.WRITING_SUB_URI && c == '>') ret = Location.PRE_PRED;
  65. else if(currLocation == Location.WRITING_BNODE_URI && c == ' ') ret = Location.PRE_PRED;
  66. else if(currLocation == Location.WRITING_PRED_URI && c == '>') ret = Location.PRE_OBJ;
  67. else if(currLocation == Location.WRITING_OBJ_URI && c == '>') ret = Location.POST_OBJ;
  68. else if(currLocation == Location.WRITING_OBJ_BNODE && c == ' ') ret = Location.POST_OBJ;
  69. else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '"') ret = Location.POST_OBJ;
  70. else if(currLocation == Location.WRITING_LIT_LANG && c == ' ') ret = Location.POST_OBJ;
  71. else if(currLocation == Location.WRITING_LIT_TYPE && c == '>') ret = Location.POST_OBJ;
  72. // Closing typed and language literal.
  73. else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '@') ret = Location.WRITING_LIT_LANG;
  74. else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '^') ret = Location.WRITING_LIT_TYPE;
  75. // Spaces.
  76. else if( c == ' ' &&
  77. (
  78. currLocation == Location.PRE_SUBJECT ||
  79. currLocation == Location.PRE_PRED ||
  80. currLocation == Location.PRE_OBJ ||
  81. currLocation == Location.POST_OBJ
  82. )
  83. ) ret = currLocation;
  84. // Reset.
  85. else if(currLocation == Location.POST_OBJ && c == '.') ret = Location.PRE_SUBJECT;
  86. // Error
  87. else ret = Location.ERROR;
  88. currState.location=ret;
  89. }
  90. return {
  91. startState: function() {
  92. return {
  93. location : Location.PRE_SUBJECT,
  94. uris : [],
  95. anchors : [],
  96. bnodes : [],
  97. langs : [],
  98. types : []
  99. };
  100. },
  101. token: function(stream, state) {
  102. var ch = stream.next();
  103. if(ch == '<') {
  104. transitState(state, ch);
  105. var parsedURI = '';
  106. stream.eatWhile( function(c) { if( c != '#' && c != '>' ) { parsedURI += c; return true; } return false;} );
  107. state.uris.push(parsedURI);
  108. if( stream.match('#', false) ) return 'variable';
  109. stream.next();
  110. transitState(state, '>');
  111. return 'variable';
  112. }
  113. if(ch == '#') {
  114. var parsedAnchor = '';
  115. stream.eatWhile(function(c) { if(c != '>' && c != ' ') { parsedAnchor+= c; return true; } return false;});
  116. state.anchors.push(parsedAnchor);
  117. return 'variable-2';
  118. }
  119. if(ch == '>') {
  120. transitState(state, '>');
  121. return 'variable';
  122. }
  123. if(ch == '_') {
  124. transitState(state, ch);
  125. var parsedBNode = '';
  126. stream.eatWhile(function(c) { if( c != ' ' ) { parsedBNode += c; return true; } return false;});
  127. state.bnodes.push(parsedBNode);
  128. stream.next();
  129. transitState(state, ' ');
  130. return 'builtin';
  131. }
  132. if(ch == '"') {
  133. transitState(state, ch);
  134. stream.eatWhile( function(c) { return c != '"'; } );
  135. stream.next();
  136. if( stream.peek() != '@' && stream.peek() != '^' ) {
  137. transitState(state, '"');
  138. }
  139. return 'string';
  140. }
  141. if( ch == '@' ) {
  142. transitState(state, '@');
  143. var parsedLang = '';
  144. stream.eatWhile(function(c) { if( c != ' ' ) { parsedLang += c; return true; } return false;});
  145. state.langs.push(parsedLang);
  146. stream.next();
  147. transitState(state, ' ');
  148. return 'string-2';
  149. }
  150. if( ch == '^' ) {
  151. stream.next();
  152. transitState(state, '^');
  153. var parsedType = '';
  154. stream.eatWhile(function(c) { if( c != '>' ) { parsedType += c; return true; } return false;} );
  155. state.types.push(parsedType);
  156. stream.next();
  157. transitState(state, '>');
  158. return 'variable';
  159. }
  160. if( ch == ' ' ) {
  161. transitState(state, ch);
  162. }
  163. if( ch == '.' ) {
  164. transitState(state, ch);
  165. }
  166. }
  167. };
  168. });
  169. CodeMirror.defineMIME("text/n-triples", "ntriples");
  170. });