Skip to content
Snippets Groups Projects
Commit b6d4350a authored by marihachi's avatar marihachi
Browse files

update core parser

parent 68e322b8
No related branches found
No related tags found
No related merge requests found
start = root {
function buildList(head, others) {
return [ head, ...others ];
}
function createTree(type, props, children) {
props = props || { };
children = children || [ ];
children = !Array.isArray(children) ? [children] : children;
return {
node: { type, props },
children: children
};
}
}
// general root
= block
/ inline
space // plain
= [ \t] // =
lineBreak
= "\n" / "\r\n" / "\r"
spacing
= space / lineBreak
// titile block
= title
/ quote
/ search
/ blockCode
title_nestContent inline
= inline = big
titleA_char
= !(title_nestContent / lineBreak / "】") c:. { return c; }
titleA_text // block: title
= t:$(titleA_char+) { return t; }
titleA_content title
= (title_nestContent / titleA_text)+ = titleA / titleB
titleA titleA
= "【" titleA_content "】" = "【" content:titleA_content "】"
{
return createTree('title', { }, content);
}
titleB_char titleA_content
= !(title_nestContent / lineBreak / "]") c:. { return c; } = (inline / titleA_text)+
titleB_text titleA_text
= t:$(titleB_char+) { return t; } = s:$(titleA_char+)
{
return createTree('text', { text: s });
}
titleB_content titleA_char
= (title_nestContent / titleB_text)+ = !(inline / "】") c:CHAR { return c; }
titleB titleB
= "[" titleB_content "]" = "[" content: titleB_content "]"
{
return createTree('title', { }, content);
}
title titleB_content
= titleA / titleB = (inline / titleB_text)+
// blockCode titleB_text
= s:$(titleB_char+)
{
return createTree('text', { text: s });
}
blockCode_char titleB_char
= !(lineBreak / "```") c:. { return c; } = !(inline / "]") c:CHAR { return c; }
blockCode_line
= t:$(blockCode_char*) lineBreak { return t; } // block: quote
// (handle the line as quote block if got a char ">" of the line head.)
quote
= head:quote_line tail:(NEWLINE tree:quote_line { return tree; })*
{
const trees = [head, ...tail];
console.log(trees.map(tree => tree.children));//.flat();
return [head, ...tail].join('\n');
}
quote_line
= ">" content:quote_content &ENDLINE { return createTree('quote', { }, content); }
// TODO: allow nesting
quote_content
= quote_text
quote_text
= s:$(CHAR+) { return createTree('text', { text: s }); }
// block: search
search
= q:search_query sp:[ \t] key:search_keyToken &ENDLINE
{
return createTree('search', {
query: q,
content: [ q, sp, key ].join('')
});
}
search_query =
head:CHAR tail:(!([ \t] search_keyToken ENDLINE) c:CHAR { return c; })*
{
return head + tail.join('');
}
search_keyToken
= "検索" / "search"i
// block: blockCode
blockCode blockCode
= "```" lineBreak blockCode_line* "```" = "```" NEWLINE lines: (!("```" ENDLINE) line:blockCode_line NEWLINE { return line; } )* "```" &ENDLINE { return lines; }
// parts // TODO: allow nesting
blockCode_line
= t:$(CHAR*) { return t; }
// plain
// = emoji
// / text
// block
// = title
// / quote
// / search
// / blockCode
// / mathBlock
// / center
// inline
// = big
// / bold
// / small
// / italic
// / strike
// / motion
// / spin
// / jump
// / flip
// / inlineCode
// / mathInline
// / mention
// / hashtag
// / url
// / link
// / plain
// root
// = block
// / inline
block // inline: big
= title
/ blockCode
inline = "inline" big
= "***" content:big_content "***"
{
return createTree('big', { }, content);
}
root big_content
= block = (big_text / inline)*
/ inline
big_text
= s:$(big_char+) { return createTree('text', { text: s }); }
big_char
= !("***") c:CHAR { return c; }
// Core rules
CHAR
= !NEWLINE c:. { return c; }
ENDLINE
= NEWLINE / EOF
NEWLINE
= "\r\n" / [\r\n]
EOF
= !.
// __ "whitespaces"
// = _+
// _ "whitespace"
// = [ \t]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment