1
0

ntriples.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. The following expression defines the defined ASF grammar transitions.
  9. pre_subject ->
  10. {
  11. ( writing_subject_uri | writing_bnode_uri )
  12. -> pre_predicate
  13. -> writing_predicate_uri
  14. -> pre_object
  15. -> writing_object_uri | writing_object_bnode |
  16. (
  17. writing_object_literal
  18. -> writing_literal_lang | writing_literal_type
  19. )
  20. -> post_object
  21. -> BEGIN
  22. } otherwise {
  23. -> ERROR
  24. }
  25. */
  26. CodeMirror.defineMode("ntriples", function() {
  27. var Location = {
  28. PRE_SUBJECT : 0,
  29. WRITING_SUB_URI : 1,
  30. WRITING_BNODE_URI : 2,
  31. PRE_PRED : 3,
  32. WRITING_PRED_URI : 4,
  33. PRE_OBJ : 5,
  34. WRITING_OBJ_URI : 6,
  35. WRITING_OBJ_BNODE : 7,
  36. WRITING_OBJ_LITERAL : 8,
  37. WRITING_LIT_LANG : 9,
  38. WRITING_LIT_TYPE : 10,
  39. POST_OBJ : 11,
  40. ERROR : 12
  41. };
  42. function transitState(currState, c) {
  43. var currLocation = currState.location;
  44. var ret;
  45. // Opening.
  46. if (currLocation == Location.PRE_SUBJECT && c == '<') ret = Location.WRITING_SUB_URI;
  47. else if(currLocation == Location.PRE_SUBJECT && c == '_') ret = Location.WRITING_BNODE_URI;
  48. else if(currLocation == Location.PRE_PRED && c == '<') ret = Location.WRITING_PRED_URI;
  49. else if(currLocation == Location.PRE_OBJ && c == '<') ret = Location.WRITING_OBJ_URI;
  50. else if(currLocation == Location.PRE_OBJ && c == '_') ret = Location.WRITING_OBJ_BNODE;
  51. else if(currLocation == Location.PRE_OBJ && c == '"') ret = Location.WRITING_OBJ_LITERAL;
  52. // Closing.
  53. else if(currLocation == Location.WRITING_SUB_URI && c == '>') ret = Location.PRE_PRED;
  54. else if(currLocation == Location.WRITING_BNODE_URI && c == ' ') ret = Location.PRE_PRED;
  55. else if(currLocation == Location.WRITING_PRED_URI && c == '>') ret = Location.PRE_OBJ;
  56. else if(currLocation == Location.WRITING_OBJ_URI && c == '>') ret = Location.POST_OBJ;
  57. else if(currLocation == Location.WRITING_OBJ_BNODE && c == ' ') ret = Location.POST_OBJ;
  58. else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '"') ret = Location.POST_OBJ;
  59. else if(currLocation == Location.WRITING_LIT_LANG && c == ' ') ret = Location.POST_OBJ;
  60. else if(currLocation == Location.WRITING_LIT_TYPE && c == '>') ret = Location.POST_OBJ;
  61. // Closing typed and language literal.
  62. else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '@') ret = Location.WRITING_LIT_LANG;
  63. else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '^') ret = Location.WRITING_LIT_TYPE;
  64. // Spaces.
  65. else if( c == ' ' &&
  66. (
  67. currLocation == Location.PRE_SUBJECT ||
  68. currLocation == Location.PRE_PRED ||
  69. currLocation == Location.PRE_OBJ ||
  70. currLocation == Location.POST_OBJ
  71. )
  72. ) ret = currLocation;
  73. // Reset.
  74. else if(currLocation == Location.POST_OBJ && c == '.') ret = Location.PRE_SUBJECT;
  75. // Error
  76. else ret = Location.ERROR;
  77. currState.location=ret;
  78. }
  79. return {
  80. startState: function() {
  81. return {
  82. location : Location.PRE_SUBJECT,
  83. uris : [],
  84. anchors : [],
  85. bnodes : [],
  86. langs : [],
  87. types : []
  88. };
  89. },
  90. token: function(stream, state) {
  91. var ch = stream.next();
  92. if(ch == '<') {
  93. transitState(state, ch);
  94. var parsedURI = '';
  95. stream.eatWhile( function(c) { if( c != '#' && c != '>' ) { parsedURI += c; return true; } return false;} );
  96. state.uris.push(parsedURI);
  97. if( stream.match('#', false) ) return 'variable';
  98. stream.next();
  99. transitState(state, '>');
  100. return 'variable';
  101. }
  102. if(ch == '#') {
  103. var parsedAnchor = '';
  104. stream.eatWhile(function(c) { if(c != '>' && c != ' ') { parsedAnchor+= c; return true; } return false;});
  105. state.anchors.push(parsedAnchor);
  106. return 'variable-2';
  107. }
  108. if(ch == '>') {
  109. transitState(state, '>');
  110. return 'variable';
  111. }
  112. if(ch == '_') {
  113. transitState(state, ch);
  114. var parsedBNode = '';
  115. stream.eatWhile(function(c) { if( c != ' ' ) { parsedBNode += c; return true; } return false;});
  116. state.bnodes.push(parsedBNode);
  117. stream.next();
  118. transitState(state, ' ');
  119. return 'builtin';
  120. }
  121. if(ch == '"') {
  122. transitState(state, ch);
  123. stream.eatWhile( function(c) { return c != '"'; } );
  124. stream.next();
  125. if( stream.peek() != '@' && stream.peek() != '^' ) {
  126. transitState(state, '"');
  127. }
  128. return 'string';
  129. }
  130. if( ch == '@' ) {
  131. transitState(state, '@');
  132. var parsedLang = '';
  133. stream.eatWhile(function(c) { if( c != ' ' ) { parsedLang += c; return true; } return false;});
  134. state.langs.push(parsedLang);
  135. stream.next();
  136. transitState(state, ' ');
  137. return 'string-2';
  138. }
  139. if( ch == '^' ) {
  140. stream.next();
  141. transitState(state, '^');
  142. var parsedType = '';
  143. stream.eatWhile(function(c) { if( c != '>' ) { parsedType += c; return true; } return false;} );
  144. state.types.push(parsedType);
  145. stream.next();
  146. transitState(state, '>');
  147. return 'variable';
  148. }
  149. if( ch == ' ' ) {
  150. transitState(state, ch);
  151. }
  152. if( ch == '.' ) {
  153. transitState(state, ch);
  154. }
  155. }
  156. };
  157. });
  158. CodeMirror.defineMIME("text/n-triples", "ntriples");