LaravelTest
195 строк · 6.9 Кб
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/*
12The following expression defines the defined ASF grammar transitions.
13
14pre_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(
22writing_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) {33if (typeof exports == "object" && typeof module == "object") // CommonJS34mod(require("../../lib/codemirror"));35else if (typeof define == "function" && define.amd) // AMD36define(["../../lib/codemirror"], mod);37else // Plain browser env38mod(CodeMirror);39})(function(CodeMirror) {40"use strict";41
42CodeMirror.defineMode("ntriples", function() {43
44var Location = {45PRE_SUBJECT : 0,46WRITING_SUB_URI : 1,47WRITING_BNODE_URI : 2,48PRE_PRED : 3,49WRITING_PRED_URI : 4,50PRE_OBJ : 5,51WRITING_OBJ_URI : 6,52WRITING_OBJ_BNODE : 7,53WRITING_OBJ_LITERAL : 8,54WRITING_LIT_LANG : 9,55WRITING_LIT_TYPE : 10,56POST_OBJ : 11,57ERROR : 1258};59function transitState(currState, c) {60var currLocation = currState.location;61var ret;62
63// Opening.64if (currLocation == Location.PRE_SUBJECT && c == '<') ret = Location.WRITING_SUB_URI;65else if(currLocation == Location.PRE_SUBJECT && c == '_') ret = Location.WRITING_BNODE_URI;66else if(currLocation == Location.PRE_PRED && c == '<') ret = Location.WRITING_PRED_URI;67else if(currLocation == Location.PRE_OBJ && c == '<') ret = Location.WRITING_OBJ_URI;68else if(currLocation == Location.PRE_OBJ && c == '_') ret = Location.WRITING_OBJ_BNODE;69else if(currLocation == Location.PRE_OBJ && c == '"') ret = Location.WRITING_OBJ_LITERAL;70
71// Closing.72else if(currLocation == Location.WRITING_SUB_URI && c == '>') ret = Location.PRE_PRED;73else if(currLocation == Location.WRITING_BNODE_URI && c == ' ') ret = Location.PRE_PRED;74else if(currLocation == Location.WRITING_PRED_URI && c == '>') ret = Location.PRE_OBJ;75else if(currLocation == Location.WRITING_OBJ_URI && c == '>') ret = Location.POST_OBJ;76else if(currLocation == Location.WRITING_OBJ_BNODE && c == ' ') ret = Location.POST_OBJ;77else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '"') ret = Location.POST_OBJ;78else if(currLocation == Location.WRITING_LIT_LANG && c == ' ') ret = Location.POST_OBJ;79else if(currLocation == Location.WRITING_LIT_TYPE && c == '>') ret = Location.POST_OBJ;80
81// Closing typed and language literal.82else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '@') ret = Location.WRITING_LIT_LANG;83else if(currLocation == Location.WRITING_OBJ_LITERAL && c == '^') ret = Location.WRITING_LIT_TYPE;84
85// Spaces.86else if( c == ' ' &&87(88currLocation == Location.PRE_SUBJECT ||89currLocation == Location.PRE_PRED ||90currLocation == Location.PRE_OBJ ||91currLocation == Location.POST_OBJ92)93) ret = currLocation;94
95// Reset.96else if(currLocation == Location.POST_OBJ && c == '.') ret = Location.PRE_SUBJECT;97
98// Error99else ret = Location.ERROR;100
101currState.location=ret;102}103
104return {105startState: function() {106return {107location : Location.PRE_SUBJECT,108uris : [],109anchors : [],110bnodes : [],111langs : [],112types : []113};114},115token: function(stream, state) {116var ch = stream.next();117if(ch == '<') {118transitState(state, ch);119var parsedURI = '';120stream.eatWhile( function(c) { if( c != '#' && c != '>' ) { parsedURI += c; return true; } return false;} );121state.uris.push(parsedURI);122if( stream.match('#', false) ) return 'variable';123stream.next();124transitState(state, '>');125return 'variable';126}127if(ch == '#') {128var parsedAnchor = '';129stream.eatWhile(function(c) { if(c != '>' && c != ' ') { parsedAnchor+= c; return true; } return false;});130state.anchors.push(parsedAnchor);131return 'variable-2';132}133if(ch == '>') {134transitState(state, '>');135return 'variable';136}137if(ch == '_') {138transitState(state, ch);139var parsedBNode = '';140stream.eatWhile(function(c) { if( c != ' ' ) { parsedBNode += c; return true; } return false;});141state.bnodes.push(parsedBNode);142stream.next();143transitState(state, ' ');144return 'builtin';145}146if(ch == '"') {147transitState(state, ch);148stream.eatWhile( function(c) { return c != '"'; } );149stream.next();150if( stream.peek() != '@' && stream.peek() != '^' ) {151transitState(state, '"');152}153return 'string';154}155if( ch == '@' ) {156transitState(state, '@');157var parsedLang = '';158stream.eatWhile(function(c) { if( c != ' ' ) { parsedLang += c; return true; } return false;});159state.langs.push(parsedLang);160stream.next();161transitState(state, ' ');162return 'string-2';163}164if( ch == '^' ) {165stream.next();166transitState(state, '^');167var parsedType = '';168stream.eatWhile(function(c) { if( c != '>' ) { parsedType += c; return true; } return false;} );169state.types.push(parsedType);170stream.next();171transitState(state, '>');172return 'variable';173}174if( ch == ' ' ) {175transitState(state, ch);176}177if( ch == '.' ) {178transitState(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});196