scripts/CodeMirror/mode/toml/toml.js (view raw)
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11})(function(CodeMirror) {
12"use strict";
13
14CodeMirror.defineMode("toml", function () {
15 return {
16 startState: function () {
17 return {
18 inString: false,
19 stringType: "",
20 lhs: true,
21 inArray: 0
22 };
23 },
24 token: function (stream, state) {
25 //check for state changes
26 if (!state.inString && ((stream.peek() == '"') || (stream.peek() == "'"))) {
27 state.stringType = stream.peek();
28 stream.next(); // Skip quote
29 state.inString = true; // Update state
30 }
31 if (stream.sol() && state.inArray === 0) {
32 state.lhs = true;
33 }
34 //return state
35 if (state.inString) {
36 while (state.inString && !stream.eol()) {
37 if (stream.peek() === state.stringType) {
38 stream.next(); // Skip quote
39 state.inString = false; // Clear flag
40 } else if (stream.peek() === '\\') {
41 stream.next();
42 stream.next();
43 } else {
44 stream.match(/^.[^\\\"\']*/);
45 }
46 }
47 return state.lhs ? "property string" : "string"; // Token style
48 } else if (state.inArray && stream.peek() === ']') {
49 stream.next();
50 state.inArray--;
51 return 'bracket';
52 } else if (state.lhs && stream.peek() === '[' && stream.skipTo(']')) {
53 stream.next();//skip closing ]
54 // array of objects has an extra open & close []
55 if (stream.peek() === ']') stream.next();
56 return "atom";
57 } else if (stream.peek() === "#") {
58 stream.skipToEnd();
59 return "comment";
60 } else if (stream.eatSpace()) {
61 return null;
62 } else if (state.lhs && stream.eatWhile(function (c) { return c != '=' && c != ' '; })) {
63 return "property";
64 } else if (state.lhs && stream.peek() === "=") {
65 stream.next();
66 state.lhs = false;
67 return null;
68 } else if (!state.lhs && stream.match(/^\d\d\d\d[\d\-\:\.T]*Z/)) {
69 return 'atom'; //date
70 } else if (!state.lhs && (stream.match('true') || stream.match('false'))) {
71 return 'atom';
72 } else if (!state.lhs && stream.peek() === '[') {
73 state.inArray++;
74 stream.next();
75 return 'bracket';
76 } else if (!state.lhs && stream.match(/^\-?\d+(?:\.\d+)?/)) {
77 return 'number';
78 } else if (!stream.eatSpace()) {
79 stream.next();
80 }
81 return null;
82 }
83 };
84});
85
86CodeMirror.defineMIME('text/x-toml', 'toml');
87
88});