all repos — NoPaste @ 29b774f090102303e43cf939b38ac2083e62d9f1

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/ntriples/ntriples.js (view raw)

  1// CodeMirror, copyright (c) by Marijn Haverbeke and others
  2// Distributed under an MIT license: https://codemirror.net/LICENSE
  3
  4/**********************************************************
  5* This script provides syntax highlighting support for
  6* the N-Triples format.
  7* N-Triples format specification:
  8*     https://www.w3.org/TR/n-triples/
  9***********************************************************/
 10
 11/*
 12    The following expression defines the defined ASF grammar transitions.
 13
 14    pre_subject ->
 15        {
 16        ( writing_subject_uri | writing_bnode_uri )
 17            -> pre_predicate
 18                -> writing_predicate_uri
 19                    -> pre_object
 20                        -> writing_object_uri | writing_object_bnode |
 21                          (
 22                            writing_object_literal
 23                                -> writing_literal_lang | writing_literal_type
 24                          )
 25                            -> post_object
 26                                -> BEGIN
 27         } otherwise {
 28             -> ERROR
 29         }
 30*/
 31
 32(function(mod) {
 33  if (typeof exports == "object" && typeof module == "object") // CommonJS
 34    mod(require("../../lib/codemirror"));
 35  else if (typeof define == "function" && define.amd) // AMD
 36    define(["../../lib/codemirror"], mod);
 37  else // Plain browser env
 38    mod(CodeMirror);
 39})(function(CodeMirror) {
 40"use strict";
 41
 42CodeMirror.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    // Opening.
 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    // Closing.
 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    // Closing typed and language literal.
 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    // Spaces.
 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    // Reset.
 96    else if(currLocation == Location.POST_OBJ && c == '.') ret = Location.PRE_SUBJECT;
 97
 98    // Error
 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// define the registered Media Type for n-triples:
185// https://www.w3.org/TR/n-triples/#n-triples-mediatype
186CodeMirror.defineMIME("application/n-triples", "ntriples");
187
188// N-Quads is based on the N-Triples format (so same highlighting works)
189// https://www.w3.org/TR/n-quads/
190CodeMirror.defineMIME("application/n-quads", "ntriples");
191
192// previously used, though technically incorrect media type for n-triples
193CodeMirror.defineMIME("text/n-triples", "ntriples");
194
195});