Created
March 5, 2018 11:27
-
-
Save okunokentaro/10e4c52ffea9fb91b5981aafafc04c1c to your computer and use it in GitHub Desktop.
This file contains 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
const _statements = [ | |
`class_implements 'Foo' and includes 'Bar' then error`, | |
`class_extends 'BaseMediator' and import 'component' then error`, | |
`method_returns 'Observable' and not name_matches '\\$$' then warn`, | |
`class_implements 'Foo' and includes 'Bar' then error`, | |
]; | |
// noprotect | |
const f = statements => { | |
console.log('==============================='); | |
Array.from(statements).reduce( | |
(acc, v, i, arr) => { | |
if (acc.mode === 'then_indicator' && arr.length - 1 === i) { | |
if (statements.slice(acc.cursor, i + 1) === 'warn') { | |
console.log(v, i, `"${statements.slice(acc.cursor, i + 1)}"`, acc.mode); | |
return { mode: 'statement', cursor: arr.length }; | |
} | |
if (statements.slice(acc.cursor, i + 1) === 'error') { | |
console.log(v, i, `"${statements.slice(acc.cursor, i + 1)}"`, acc.mode); | |
return { mode: 'statement', cursor: arr.length }; | |
} | |
} | |
if (acc.mode === 'statement') { | |
if (arr.length - 1 === i) { | |
throw new Error('parse error') | |
} | |
if (v === 'c') { | |
// class_implements, class_extends | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'indicator', cursor: i }; | |
} | |
if (v === 'm') { | |
// method_returns | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'indicator', cursor: i }; | |
} | |
throw new Error(''); | |
} | |
if (acc.mode === 'indicator') { | |
if (arr.length - 1 === i) { | |
throw new Error('parse error') | |
} | |
if (v === ' ') { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'start_identifier', cursor: i + 1 }; | |
} | |
} | |
if (acc.mode === 'start_identifier') { | |
if (arr.length - 1 === i) { | |
throw new Error('parse error') | |
} | |
if (v === "'") { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'identifier', cursor: i + 1 }; | |
} | |
throw new Error(''); | |
} | |
if (acc.mode === 'and_then_statement') { | |
if (arr.length - 1 === i) { | |
throw new Error('parse error') | |
} | |
if (v === 'a') { | |
// and | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'and_indicator', cursor: i }; | |
} | |
if (v === "'") { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'identifier', cursor: i + 1 }; | |
} | |
if (v === 't') { | |
// then | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'then_indicator', cursor: i }; | |
} | |
if (v === ' ') { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'and_then_statement', cursor: i + 1 }; | |
} | |
throw new Error(''); | |
} | |
if (acc.mode === 'and_statement') { | |
if (arr.length - 1 === i) { | |
throw new Error('parse error') | |
} | |
if (v === 'i') { | |
// includes, import | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'and_indicator', cursor: i }; | |
} | |
if (v === 'n') { | |
// not | |
if (statements.slice(acc.cursor, i) === 'not') { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'and_indicator', cursor: i }; | |
} | |
if (statements.slice(acc.cursor, i) === 'name_matches') { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'and_statement', cursor: i }; | |
} | |
} | |
if (v === "'") { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'identifier', cursor: i + 1 }; | |
} | |
if (v === ' ') { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'and_statement', cursor: i + 1 }; | |
} | |
} | |
if (acc.mode === 'and_indicator') { | |
if (arr.length - 1 === i) { | |
throw new Error('parse error') | |
} | |
if (v === ' ') { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'and_statement', cursor: i + 1 }; | |
} | |
} | |
if (acc.mode === 'then_indicator') { | |
if (arr.length - 1 === i) { | |
throw new Error('parse error') | |
} | |
if (v === ' ') { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'then_statement', cursor: i + 1 }; | |
} | |
} | |
if (acc.mode === 'then_statement') { | |
if (arr.length - 1 === i) { | |
throw new Error('parse error') | |
} | |
if (v === 'e') { | |
// error | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'then_indicator', cursor: i }; | |
} | |
if (v === 'w') { | |
// warn | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'then_indicator', cursor: i }; | |
} | |
} | |
if (acc.mode === 'identifier') { | |
if (arr.length - 1 === i) { | |
throw new Error('parse error') | |
} | |
if (v === "'") { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'end_identifier', cursor: i }; | |
} | |
} | |
if (acc.mode === 'end_identifier') { | |
if (arr.length - 1 === i) { | |
throw new Error('parse error') | |
} | |
if (v === ' ') { | |
console.log(v, i, `"${statements.slice(acc.cursor, i)}"`, acc.mode); | |
return { mode: 'and_then_statement', cursor: i + 1 }; | |
} | |
} | |
return acc; | |
}, | |
{ mode: 'statement', cursor: 0 }, | |
); | |
}; | |
f(_statements[0]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment