scripts/CodeMirror/mode/http/http.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("http", function() {
15 function failFirstLine(stream, state) {
16 stream.skipToEnd();
17 state.cur = header;
18 return "error";
19 }
20
21 function start(stream, state) {
22 if (stream.match(/^HTTP\/\d\.\d/)) {
23 state.cur = responseStatusCode;
24 return "keyword";
25 } else if (stream.match(/^[A-Z]+/) && /[ \t]/.test(stream.peek())) {
26 state.cur = requestPath;
27 return "keyword";
28 } else {
29 return failFirstLine(stream, state);
30 }
31 }
32
33 function responseStatusCode(stream, state) {
34 var code = stream.match(/^\d+/);
35 if (!code) return failFirstLine(stream, state);
36
37 state.cur = responseStatusText;
38 var status = Number(code[0]);
39 if (status >= 100 && status < 200) {
40 return "positive informational";
41 } else if (status >= 200 && status < 300) {
42 return "positive success";
43 } else if (status >= 300 && status < 400) {
44 return "positive redirect";
45 } else if (status >= 400 && status < 500) {
46 return "negative client-error";
47 } else if (status >= 500 && status < 600) {
48 return "negative server-error";
49 } else {
50 return "error";
51 }
52 }
53
54 function responseStatusText(stream, state) {
55 stream.skipToEnd();
56 state.cur = header;
57 return null;
58 }
59
60 function requestPath(stream, state) {
61 stream.eatWhile(/\S/);
62 state.cur = requestProtocol;
63 return "string-2";
64 }
65
66 function requestProtocol(stream, state) {
67 if (stream.match(/^HTTP\/\d\.\d$/)) {
68 state.cur = header;
69 return "keyword";
70 } else {
71 return failFirstLine(stream, state);
72 }
73 }
74
75 function header(stream) {
76 if (stream.sol() && !stream.eat(/[ \t]/)) {
77 if (stream.match(/^.*?:/)) {
78 return "atom";
79 } else {
80 stream.skipToEnd();
81 return "error";
82 }
83 } else {
84 stream.skipToEnd();
85 return "string";
86 }
87 }
88
89 function body(stream) {
90 stream.skipToEnd();
91 return null;
92 }
93
94 return {
95 token: function(stream, state) {
96 var cur = state.cur;
97 if (cur != header && cur != body && stream.eatSpace()) return null;
98 return cur(stream, state);
99 },
100
101 blankLine: function(state) {
102 state.cur = body;
103 },
104
105 startState: function() {
106 return {cur: start};
107 }
108 };
109});
110
111CodeMirror.defineMIME("message/http", "http");
112
113});