Skip to content

Commit

Permalink
update parser
Browse files Browse the repository at this point in the history
* implement fn
  • Loading branch information
marihachi committed Mar 20, 2021
1 parent 7cb3b43 commit 8516055
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/cli/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ async function entryPoint() {
// replace special chars
input = input
.replace(/\\n/g, '\n')
.replace(/\\t/g, '\t');
.replace(/\\t/g, '\t')
.replace(/\\u00a0/g, '\u00a0');

let result: any;
try {
Expand Down
41 changes: 37 additions & 4 deletions src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ inline
/ hashtag
/ url
/ link
/ fn
/ text

// inline: emoji
Expand Down Expand Up @@ -200,7 +201,7 @@ bold
{
return createNode('bold', { }, mergeText(content));
}
/ "__" content:$(!"__" c:[a-z0-9 \t]i { return c; })+ "__"
/ "__" content:$(!"__" c:([a-z0-9]i / _) { return c; })+ "__"
{
const parsedContent = applyParser(content, 'inlineParser');
return createNode('bold', { }, parsedContent);
Expand All @@ -221,12 +222,12 @@ italic
{
return createNode('italic', { }, mergeText(content));
}
/ "*" content:$(!"*" [a-z0-9 \t]i)+ "*"
/ "*" content:$(!"*" ([a-z0-9]i / _))+ "*"
{
const parsedContent = applyParser(content, 'inlineParser');
return createNode('italic', { }, parsedContent);
}
/ "_" content:$(!"_" [a-z0-9 \t]i)+ "_"
/ "_" content:$(!"_" ([a-z0-9]i / _))+ "_"
{
const parsedContent = applyParser(content, 'inlineParser');
return createNode('italic', { }, parsedContent);
Expand Down Expand Up @@ -358,6 +359,38 @@ linkLabel
linkUrl
= url { return text(); }

// inline: fn

fn
= "[" name:$([a-z0-9_]i)+ args:fnArgs? _ content:(!"]" i:inline { return i; })+ "]"
{
args = args || {};
return createNode('fn', {
name: name,
args: args
}, mergeText(content));
}

fnArgs
= "." head:fnArg tails:("," arg:fnArg { return arg; })*
{
const args = { };
for (const pair of [head, ...tails]) {
args[pair.k] = pair.v;
}
return args;
}

fnArg
= k:$([a-z0-9_]i)+ "=" v:$([a-z0-9_]i)+
{
return { k, v };
}
/ k:$([a-z0-9_]i)+
{
return { k: k, v: true };
}

// inline: text

text
Expand All @@ -383,4 +416,4 @@ LF
= "\r\n" / [\r\n]

_ "whitespace"
= [  \t]
= [  \t\u00a0]

0 comments on commit 8516055

Please sign in to comment.