You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
6.9 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. /**********************************************************
  4. * This script provides syntax highlighting support for
  5. * the N-Triples format.
  6. * N-Triples format specification:
  7. * https://www.w3.org/TR/n-triples/
  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. // define the registered Media Type for n-triples:
  170. // https://www.w3.org/TR/n-triples/#n-triples-mediatype
  171. CodeMirror.defineMIME("application/n-triples", "ntriples");
  172. // N-Quads is based on the N-Triples format (so same highlighting works)
  173. // https://www.w3.org/TR/n-quads/
  174. CodeMirror.defineMIME("application/n-quads", "ntriples");
  175. // previously used, though technically incorrect media type for n-triples
  176. CodeMirror.defineMIME("text/n-triples", "ntriples");
  177. });