Created
December 14, 2024 10:18
-
-
Save sergeche/b1bd9d45ee1279541e1e42c01f1d9bc2 to your computer and use it in GitHub Desktop.
Пример минималистичного парсера
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse(text) { | |
const state = { text, pos: 0 } | |
const tokens = [] | |
while (state.pos < text.length) { | |
const token = word(state) || delimiter(state) | |
if (!token) { | |
throw new Error(`Unknown character at ${state.pos}`) | |
} | |
tokens.push(token) | |
} | |
return tokens | |
} | |
function word(state) { | |
const start = state.pos | |
while (state.pos < state.text.length) { | |
if (!isWordChar(state.text.charCodeAt(state.pos))) { | |
break | |
} | |
state.pos++ | |
} | |
if (start !== state.pos) { | |
return { | |
type: 'word', | |
value: state.text.slice(start, state.pos) | |
} | |
} | |
} | |
function delimiter(state) { | |
const ch = state.text.charCodeAt(state.pos) | |
if (ch === 32 /* пробел */ || ch === 44 /* , */) { | |
return { | |
type: 'delimiter', | |
value: state.text[state.pos++] | |
} | |
} | |
} | |
function isWordChar(ch) { | |
return ch >= 97 && ch <= 122 /* a-z */ | |
} | |
console.log(parse('hello world')) | |
// [ | |
// { type: 'word', value: 'hello' }, | |
// { type: 'delimiter', value: ' ' }, | |
// { type: 'word', value: 'world' } | |
// ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment