actually using new parser, etc.
authorNicholas Bollweg <nicholas.bollweg@gtri.gatech.edu>
Tue, 22 Oct 2013 14:30:05 -0400
changeset 2076 df655b10a4eb
parent 2075 7f688a6faf03
child 2077 134f64d36775
actually using new parser, etc.
playground/codemirror.jsonld.js
playground/index.html
playground/playground.js
--- a/playground/codemirror.jsonld.js	Tue Oct 22 14:12:30 2013 -0400
+++ b/playground/codemirror.jsonld.js	Tue Oct 22 14:30:05 2013 -0400
@@ -28,7 +28,8 @@
 CodeMirror.defineMode("javascript", function(config, parserConfig) {
   var indentUnit = config.indentUnit;
   var statementIndent = parserConfig.statementIndent;
-  var jsonMode = parserConfig.json;
+  var jsonldMode = parserConfig.jsonld;
+  var jsonMode = parserConfig.json || jsonldMode;
   var isTS = parserConfig.typescript;
 
   // Tokenizer
@@ -161,6 +162,7 @@
       return ret("string", "string");
     };
   }
+  
 
   function jsTokenComment(stream, state) {
     var maybeEnd = false, ch;
@@ -178,6 +180,46 @@
 
   var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true};
 
+
+  // patches for JSON-LD
+  if(jsonldMode){
+    (function(){
+      var ldAtomicTypes = ["jsonld-keyword", "jsonld-uri"],
+        ldKeywords = "context|id|value|language|type|container|list|"+
+          "set|reverse|index|base|vocab|graph",
+        ldEndQuote = "(?=\")",
+        ldKeywordRE = new RegExp("^@(" + ldKeywords + ")" + ldEndQuote),
+        ldUriRE = new RegExp("https?://[^\"]+" + ldEndQuote);
+      
+      for(var atom in ldAtomicTypes){
+        atomicTypes[atom] = true;
+      }
+      
+      jsTokenString = function(quote){
+        var ty_st, ch;
+        return function(stream, state) {
+          ty_st = null;
+          
+          // check out the first character to avoid doing regex every time
+          ch = stream.peek();
+          
+          if(ch === "@" && stream.match(ldKeywordRE)){
+            ty_st = ret("jsonld-keyword", "keyword");
+          }
+          else if(ch === "h" && stream.match(ldUriRE)){
+            ty_st = ret("jsonld-uri", "string-2");
+          }
+          
+          // consume the quote, reset the tokenizer
+          if (!nextUntilUnescaped(stream, quote))
+            state.tokenize = jsTokenBase;
+          
+          return ty_st || ret("string", "string");
+        };
+      };
+    })();
+  }
+
   function JSLexical(indented, column, type, align, prev, info) {
     this.indented = indented;
     this.column = column;
@@ -501,5 +543,6 @@
 CodeMirror.defineMIME("application/ecmascript", "javascript");
 CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
 CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
+CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true});
 CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
 CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });
\ No newline at end of file
--- a/playground/index.html	Tue Oct 22 14:12:30 2013 -0400
+++ b/playground/index.html	Tue Oct 22 14:30:05 2013 -0400
@@ -262,7 +262,7 @@
     <script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/3.16.0/addon/lint/json-lint.js"></script>
     <script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/3.16.0/addon/edit/matchbrackets.js"></script>
     <script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/3.16.0/addon/display/placeholder.js"></script>
-    <script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/3.16.0/mode/javascript/javascript.js"></script>
+    <script src="./codemirror.jsonld.js"></script>
     
     <script type="text/javascript">
       $('#frame-div').hide();
--- a/playground/playground.js	Tue Oct 22 14:12:30 2013 -0400
+++ b/playground/playground.js	Tue Oct 22 14:30:05 2013 -0400
@@ -174,7 +174,7 @@
         lineNumbers: true,
         matchBrackets: true,
         lineWrapping: true,
-        mode: "application/json",
+        mode: "application/ld+json",
         gutters: ["CodeMirror-lint-markers"],
         theme: "elegant",
         lint: true