1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CodeMirror Example</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.css">
<style>
.CodeMirror {
font-family: monospace;
height: 300px;
border: 1px solid #ccc;
}
.cm-brace {
color: blue;
}
.cm-if {
color: purple;
}
.cm-boolean {
color: aqua;
}
.cm-variable-2 {
color: green;
}
.cm-atom {
color: orange;
}
.cm-number {
color: teal;
}
.cm-string {
color: brown;
}
.cm-bracket {
color: red;
}
.cm-paren {
color: darkred;
}
.cm-punctuation {
color: black;
}
.cm-operator {
color: gray;
}
</style>
</head>
<body>
<textarea id="codeBox" rows="10" cols="30">
blabla blabla {IF myFunctionOne("aa", 2, ["a", 2, "b"]) > 3} blablabla {eNdiF} {myFunctionTwo(3, 5, "ee\n\n\"\"\\")} more text outside of braces {NOT(count([2+3])>0}
</textarea>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.js"></script>
<script>
// Définir le mode de votre langage
CodeMirror.defineMode("ldt", function(config, parserConfig) {
return {
startState: function() {
return {
inCode: false,
inString: false,
escaped: false
};
},
token: function(stream, state) {
// Gestion des accolades
if (stream.match(/\{/)) {
state.inCode = true;
stream.eatSpace();
return "brace";
}
if (state.inCode) {
console.log("in code");
if (stream.match(/\}/)) {
state.inCode = false;
return "brace";
}
else if (stream.match(/\"/)) {
while(typeof(stream.peek()) !== "undefined"){//continue jusqu'à la fin du fichier
console.log(stream.peek());
if (stream.match(/\\\\/)) {
}
else if (stream.match(/\\n/)) {
}
else
if (stream.match(/\\\"/)) {
} else if (stream.match(/\"/)) {
state.inString = false;
return "string";
} else {
stream.next();
}
if (typeof(stream.peek()) === "undefined")
return "string";
}
return "string";
} else {
// Gestion des différents types de tokens à l'intérieur des accolades
if (stream.match(/(true|false)\b/i)) {
return "boolean";
} if (stream.match(/(if|else|endif)\b/i)) {
return "if";
} else if (stream.match(/(\+|\-|\*|\/|>|<|=|<=|>=|<>|NOT|AND|XOR|OR)\b/i)) {
// Gestion des opérateurs
return "operator";
}else
if (stream.match(/[a-zA-Z_]\w*\b/)) {
// Gestion des noms de fonction (convertir en minuscules)
return "variable-2";
} else if (stream.match(/[-+]?\d*\.\d+([eE][-+]?\d+)?\b/)) {
return "number";
} else if (stream.match(/[-+]?\d+\b/)) {
return "number";
} else if (stream.match(/[()]/)) {
// Gestion des parenthèses
return "paren";
} else if (stream.match(/[\[\]]/)) {
// Gestion des crochets
return "bracket";
} else if (stream.match(/,/)) {
// Gestion des virgules
return "punctuation";
} else if (stream.match(/\n/)) {
// Gestion des sauts de ligne
stream.eatSpace();
return null;
} else {
stream.next();
return null;
}
}
}
// Traiter le reste du texte sans coloration
stream.next();
return null;
}
};
});
// Utilisation du mode dans CodeMirror
var editor = CodeMirror.fromTextArea(document.getElementById("codeBox"), {
mode: "ldt",
lineNumbers: true,
theme: "default" // Utilisation du thème par défaut de CodeMirror
});
</script>
</body>
</html> |
Partager