Skip to content

Commit

Permalink
refactor: attempt to reuse grammar and lexer for simple tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
customcommander committed Jun 28, 2020
1 parent 2c398e5 commit 4d4629e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 26 deletions.
12 changes: 2 additions & 10 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function id(x) { return x[0]; }
{"name": "OTHER_TAG$subexpression$1", "symbols": ["AUTHOR2"]},
{"name": "OTHER_TAG$subexpression$1", "symbols": ["AUTHOR3"]},
{"name": "OTHER_TAG$subexpression$1", "symbols": ["AUTHOR4"]},
{"name": "OTHER_TAG$subexpression$1", "symbols": ["SINGLE_LINE_ENTRY"]},
{"name": "OTHER_TAG$subexpression$1", "symbols": ["TAG_ENTRY"]},
{"name": "OTHER_TAG", "symbols": ["OTHER_TAG$subexpression$1"], "postprocess": ([[d]]) => d},
{"name": "ABSTRACT", "symbols": [(lexer.has("AB") ? {type: "AB"} : AB), (lexer.has("SEP") ? {type: "SEP"} : SEP), (lexer.has("CONTENT") ? {type: "CONTENT"} : CONTENT), "__"], "postprocess": ([,,{value}]) => ({key: 'abstract' , value})},
{"name": "AUTHOR_ADDR", "symbols": [(lexer.has("AD") ? {type: "AD"} : AD), (lexer.has("SEP") ? {type: "SEP"} : SEP), (lexer.has("CONTENT") ? {type: "CONTENT"} : CONTENT), "__"], "postprocess": ([,,{value}]) => ({key: 'author_address', value})},
Expand All @@ -60,15 +60,7 @@ function id(x) { return x[0]; }
{"name": "AUTHOR2", "symbols": [(lexer.has("A2") ? {type: "A2"} : A2), (lexer.has("SEP") ? {type: "SEP"} : SEP), (lexer.has("NAME_CONTENT") ? {type: "NAME_CONTENT"} : NAME_CONTENT), "__"], "postprocess": ([,,{value}]) => ({key: 'author_sec' , value})},
{"name": "AUTHOR3", "symbols": [(lexer.has("A3") ? {type: "A3"} : A3), (lexer.has("SEP") ? {type: "SEP"} : SEP), (lexer.has("NAME_CONTENT") ? {type: "NAME_CONTENT"} : NAME_CONTENT), "__"], "postprocess": ([,,{value}]) => ({key: 'author_ter' , value})},
{"name": "AUTHOR4", "symbols": [(lexer.has("A4") ? {type: "A4"} : A4), (lexer.has("SEP") ? {type: "SEP"} : SEP), (lexer.has("NAME_CONTENT") ? {type: "NAME_CONTENT"} : NAME_CONTENT), "__"], "postprocess": ([,,{value}]) => ({key: 'author_sub' , value})},
{"name": "BT_ENTRY", "symbols": [(lexer.has("BT") ? {type: "BT"} : BT), (lexer.has("SEP") ? {type: "SEP"} : SEP), (lexer.has("CONTENT") ? {type: "CONTENT"} : CONTENT), "__"], "postprocess": ([,,{value}]) => ({key: 'bt' , value})},
{"name": "SINGLE_LINE_ENTRY$subexpression$1", "symbols": [(lexer.has("BT") ? {type: "BT"} : BT)]},
{"name": "SINGLE_LINE_ENTRY$subexpression$1", "symbols": [(lexer.has("CP") ? {type: "CP"} : CP)]},
{"name": "SINGLE_LINE_ENTRY$subexpression$1", "symbols": [(lexer.has("CT") ? {type: "CT"} : CT)]},
{"name": "SINGLE_LINE_ENTRY$subexpression$1", "symbols": [(lexer.has("ED") ? {type: "ED"} : ED)]},
{"name": "SINGLE_LINE_ENTRY$subexpression$1", "symbols": [(lexer.has("EP") ? {type: "EP"} : EP)]},
{"name": "SINGLE_LINE_ENTRY$subexpression$1", "symbols": [(lexer.has("ID") ? {type: "ID"} : ID)]},
{"name": "SINGLE_LINE_ENTRY$subexpression$1", "symbols": [(lexer.has("IS") ? {type: "IS"} : IS)]},
{"name": "SINGLE_LINE_ENTRY", "symbols": ["SINGLE_LINE_ENTRY$subexpression$1", (lexer.has("SEP") ? {type: "SEP"} : SEP), (lexer.has("CONTENT") ? {type: "CONTENT"} : CONTENT), "__"], "postprocess": ([[{value: key}],,{value}]) => ({key, value})},
{"name": "TAG_ENTRY", "symbols": [(lexer.has("TAG") ? {type: "TAG"} : TAG), (lexer.has("SEP") ? {type: "SEP"} : SEP), (lexer.has("CONTENT") ? {type: "CONTENT"} : CONTENT), "__"], "postprocess": ([tag,,content]) => ({key: tag.value, value: content.value})},
{"name": "KEYWORD$ebnf$1", "symbols": ["LINE"]},
{"name": "KEYWORD$ebnf$1", "symbols": ["KEYWORD$ebnf$1", "LINE"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}},
{"name": "KEYWORD", "symbols": [(lexer.has("KW") ? {type: "KW"} : KW), (lexer.has("SEP") ? {type: "SEP"} : SEP), "KEYWORD$ebnf$1"], "postprocess": ([,,lines]) =>
Expand Down
11 changes: 2 additions & 9 deletions grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ OTHER_TAG -> ( KEYWORD
| AUTHOR2
| AUTHOR3
| AUTHOR4
| SINGLE_LINE_ENTRY
| TAG_ENTRY
)
{% ([[d]]) => d %}

Expand All @@ -56,15 +56,8 @@ AUTHOR1 -> %AU %SEP %NAME_CONTENT __ {% ([,,{value}]) => ({key: 'author'
AUTHOR2 -> %A2 %SEP %NAME_CONTENT __ {% ([,,{value}]) => ({key: 'author_sec' , value}) %}
AUTHOR3 -> %A3 %SEP %NAME_CONTENT __ {% ([,,{value}]) => ({key: 'author_ter' , value}) %}
AUTHOR4 -> %A4 %SEP %NAME_CONTENT __ {% ([,,{value}]) => ({key: 'author_sub' , value}) %}
BT_ENTRY -> %BT %SEP %CONTENT __ {% ([,,{value}]) => ({key: 'bt' , value}) %}

SINGLE_LINE_ENTRY ->
(%BT | %CP | %CT | %ED | %EP | %ID | %IS)
%SEP
%CONTENT __
{% ([[{value: key}],,{value}]) => ({key, value}) %}


TAG_ENTRY -> %TAG %SEP %CONTENT __ {% ([tag,,content]) => ({key: tag.value, value: content.value}) %}

KEYWORD -> %KW %SEP LINE:+
{% ([,,lines]) =>
Expand Down
8 changes: 1 addition & 7 deletions lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const lexer =
, A3: /A3(?= - )/
, A4: /A4(?= - )/
, AV: /AV(?= - )/
, BT: /BT(?= - )/
, C1: /C1(?= - )/
, C2: /C2(?= - )/
, C3: /C3(?= - )/
Expand All @@ -50,24 +49,19 @@ const lexer =
, C8: /C8(?= - )/
, CA: /CA(?= - )/
, CN: /CN(?= - )/
, CP: /CP(?= - )/
, CT: /CT(?= - )/
, CY: /CY(?= - )/
, DA: /DA(?= - )/
, DB: /DB(?= - )/
, DO: /DO(?= - )/
, DP: /DP(?= - )/
, ED: /ED(?= - )/
, EP: /EP(?= - )/
, ET: /ET(?= - )/
, ID: /ID(?= - )/
, IS: /IS(?= - )/
, J2: /J2(?= - )/
, KW: /KW(?= - )/
, PY: /PY(?= - )/
, RP: /RP(?= - )/
, UR: /UR(?= - )/
, ER: /ER(?= - )/
, TAG: /(?:BT|CP|CT|ED|EP|ID|IS)(?= - )/
/* <<< */
, TY_VAL: [ "ABST" , "ADVS" , "AGGR"
, "ANCIENT", "ART" , "BILL"
Expand Down

0 comments on commit 4d4629e

Please sign in to comment.