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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| nginx-parser.js
// nginx config lexer + parser (browser version) // 用法: // import { parseNginx } from './nginx-parser.js'; // const payload = parseNginx(nginxText, { filename:'nginx.conf', comments:true });
const EXTERNAL_PARSERS = {}; // 预留第三方指令解析器
// ---------- lexer ---------- function* lex(text, filename = 'nginx.conf') { let line = 1, col = 0, idx = 0; const len = text.length;
function skipWhitespace() { while (idx < len && /\s/.test(text[idx])) { if (text[idx] === '\n') { line++; col = 0; } else col++; idx++; } }
function readString(quote) { const start = ++idx; // skip open quote col++; while (idx < len && text[idx] !== quote) { if (text[idx] === '\n') { line++; col = 0; } else if (text[idx] === '\\') { idx++; col++; } // skip escaped idx++; col++; } if (idx >= len) throw new Error(`Unterminated quoted string at ${filename}:${line}`); const val = text.slice(start, idx); idx++; col++; // skip close quote return [val, line, true]; // [token, lineno, quoted] }
function readToken() { const start = idx; while (idx < len && !/\s|[{};#"'\\]/.test(text[idx])) { idx++; col++; } return text.slice(start, idx); }
while (idx < len) { skipWhitespace(); if (idx >= len) break; const ch = text[idx];
if (ch === '#' || ch === ';' || ch === '{' || ch === '}') { const tok = ch === '#' ? text.slice(idx, text.indexOf('\n', idx)) || text.slice(idx) : ch; yield [tok, line, false]; if (tok === '\n') { line++; col = 0; } else col++; idx += tok.length; } else if (ch === '"' || ch === "'") { yield readString(ch); } else { const tok = readToken(); if (tok) yield [tok, line, false]; } } }
// ---------- util ---------- class NgxParserDirectiveError extends Error { constructor(msg, lineno) { super(msg); this.name = 'NgxParserDirectiveError'; this.lineno = lineno; } }
// 简易版 analyze,只检查末尾符号 function analyze({ fname, stmt, term, ctx, strict, check_ctx, check_args }) { const dir = stmt.directive; if (dir === '#') return; // 注释跳过 if (dir.startsWith('#')) return; // 行内注释也跳过 // 更复杂的上下文/参数检查可在此扩展 if (term !== ';' && term !== '{' && term !== '}') { throw new NgxParserDirectiveError( `directive "${dir}" is not terminated by ";" or "{"`, stmt.line); } }
function enterBlockCtx(stmt, ctx) { // 返回新的上下文,简单拼路径 return [...ctx, stmt.directive]; }
// ---------- parser ---------- export function parseNginx(text, { filename = 'nginx.conf', onerror = null, catchErrors = true, ignore = [], single = false, comments = false, strict = false, checkCtx = true, checkArgs = true } = {}) { const tokens = [...lex(text, filename)]; // 先全部展开,方便 next() let tokIdx = 0; function next() { if (tokIdx >= tokens.length) return [null, -1, false]; return tokens[tokIdx++]; } function peek() { if (tokIdx >= tokens.length) return [null, -1, false]; return tokens[tokIdx]; }
const payload = { status: 'ok', errors: [], config: [] };
function handleError(parsing, e) { const file = parsing.file; const errObj = { file, error: e.message, line: e.lineno || null }; if (onerror) errObj.callback = onerror(e);
parsing.status = 'failed'; parsing.errors.push({ error: e.message, line: e.lineno || null });
payload.status = 'failed'; payload.errors.push(errObj); }
function _parse(parsing, ctx = [], consume = false) { const fname = parsing.file; const parsed = [];
while (true) { const [token, lineno, quoted] = next(); if (!token) break; if (token === '}' && !quoted) break; if (consume) { if (token === '{' && !quoted) _parse(parsing, ctx, true); continue; }
const directive = token;
// 注释行 if (directive.startsWith('#') && !quoted) { if (comments) { parsed.push({ directive: '#', line: lineno, args: [], comment: directive.slice(1).trim() }); } continue; }
const stmt = { directive, line: lineno, args: [] }; if (!single) stmt.file = fname;
// 收集参数 const commentsInArgs = []; let term; while (true) { const [t, , q] = peek(); if (!t || (!q && [';', '{', '}'].includes(t))) { term = t; next(); break; } next(); if (t.startsWith('#') && !q) commentsInArgs.push(t.slice(1).trim()); else stmt.args.push(t); }
if (ignore.includes(stmt.directive)) { if (term === '{' && !quoted) _parse(parsing, ctx, true); continue; }
// if 特殊处理 if (stmt.directive === 'if') { const args = stmt.args; if (args.length && args[0].startsWith('(') && args[args.length - 1].endsWith(')')) { args[0] = args[0].slice(1).trimLeft(); args[args.length - 1] = args[args.length - 1].slice(0, -1).trimRight(); } }
try { analyze({ fname, stmt, term, ctx, strict, check_ctx: checkCtx, check_args: checkArgs }); } catch (e) { if (catchErrors) { handleError(parsing, e); if (e.message.includes('is not terminated') && term !== '}' && !quoted) { _parse(parsing, ctx, true); } continue; } else { throw e; } }
// include 处理(浏览器端不做文件 IO,只留空壳) if (!single && stmt.directive === 'include') { stmt.includes = []; // 实际文件列表无法获取,留空 }
if (term === '{' && !quoted) { const inner = enterBlockCtx(stmt, ctx); stmt.block = _parse(parsing, inner); }
parsed.push(stmt);
// 行内注释追加 commentsInArgs.forEach(c => parsed.push({ directive: '#', line: stmt.line, args: [], comment: c })); } return parsed; }
// 主流程 const parsing = { file: filename, status: 'ok', errors: [], parsed: [] }; try { parsing.parsed = _parse(parsing, []); } catch (e) { handleError(parsing, e); } payload.config.push(parsing); return payload; }
// 预留外部解析器注册 export function registerExternalParser(parser, directives) { directives.forEach(d => { EXTERNAL_PARSERS[d] = parser; }); }
|