Reputation: 15336
This grammar
block = ph+ space* end
ph = space* ph_line+
ph_line = (~"\\n\\n" any)+ "\\n"
can't parse this text
const text = `
# Title
some
`
Error
error: Can't parse block: Line 5, col 1:
4 | some
> 5 |
^
Expected "\n"
at parse_block (/Users/alex/base/projects/ts/play.js:30:33)
at /Users/alex/base/projects/ts/play.js:34:11
Full code
const ohm = require('ohm-js')
const block_grammar = ohm.grammar(`
Markdown {
block = ph+ space* end
ph = space* ph_line+
ph_line = (~"\\n\\n" any)+ "\\n"
}
`)
const block_semantics = block_grammar.createSemantics().addOperation('parse_block', {
block(items, _space, _postfix) {
return {
items: items.children.map((c) => c.parse_block()),
tags: tags.children.map((c) => c.parse_block())
} },
ph(_space, lines) { return {
type: 'ph',
lines: lines.children.map((it) => it.parse_block())
} },
ph_line(text, _postfix) { return text.sourceString },
_terminal() { return this.sourceString },
})
export function parse_block(text) {
text = "\n" + text.trim() + "\n"
const match = block_grammar.match(text)
if (!match.succeeded()) throw new Error(`Can't parse block: ${match.message}`);
return block_semantics(match).blocks()
}
const b = parse_block(`
# Title
some
`)
console.log(b)
Upvotes: 0
Views: 33