Skip to content

Commit

Permalink
fix(pintora-diagrams): [erDiagram] allow trailing spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
hikerpig committed Feb 12, 2022
1 parent ab0cf5e commit ff1ef0c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ activityDiagram

it('can parse fork sentence', () => {
const example = stripStartEmptyLines(`
activityDiagram
activityDiagram
fork
:action 1;
forkagain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ function extractChildren(o) {
%}

start -> __ start {% (d) => d[1] %}
| "activityDiagram" document __:? {%
| "activityDiagram" _ document __:? {%
function(d) {
return d[1]
return d[2]
}
%}

Expand All @@ -84,7 +84,7 @@ line ->

statement ->
action
| ("start"|"stop"|"end"|"detach"|"kill") %NL {%
| ("start"|"stop"|"end"|"detach"|"kill") %WS:? %NL {%
function(d) {
const keyword = tv(d[0][0])
return {
Expand Down Expand Up @@ -127,7 +127,7 @@ elseClause ->
%}

whileSentence ->
"while" __ wordsInParens (%WS "is" __ wordsInParens):? _ %NL line:* %WS:* "endwhile" (%WS wordsInParens):? %NL {%
"while" __ wordsInParens (%WS "is" __ wordsInParens):? _ %NL line:* %WS:* "endwhile" (%WS wordsInParens):? %WS:? %NL {%
function(d) {
// console.log('[whileSentence]', d[6])
const confirmLabel = d[3] ? d[3][3]: undefined
Expand Down
10 changes: 5 additions & 5 deletions packages/pintora-diagrams/src/er/__tests__/er-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ erDiagram
INTEGER ArtistId
NVARCHAR Name
}
albums
albums
artists ||--o{ albums : "foreign key"
artists ||--o{ albums : "foreign key"
`)
parse(example)
const ir = db.getDiagramIR()
Expand All @@ -26,9 +26,9 @@ erDiagram
it('will parse attribute key', () => {
const example = `erDiagram
ORDER {
int orderNumber PK
string deliveryAddress
}
int orderNumber PK
string deliveryAddress
}
`
parse(example)
const ir = db.getDiagramIR()
Expand Down
15 changes: 8 additions & 7 deletions packages/pintora-diagrams/src/er/parser/erDiagram.ne
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ErDb } from '../db'

let lexer = moo.compile({
NL: { match: /\n/, lineBreaks: true },
WS: {match: /\s+/, lineBreaks: true},
WS: { match: / +/, lineBreaks: false },
WORD: /\"[^"]*\"/,
ZERO_OR_ONE: /\|o|o\|/,
ZERO_OR_MORE: /\}o|o\{/,
Expand Down Expand Up @@ -53,15 +53,15 @@ line ->
| %NL

statement ->
entityName _ relSpec _ entityName _ %COLON _ role
entityName _ relSpec _ entityName _ %COLON _ role %WS:* %NL
{%
function(d) {
yy.addEntity(tv(d[0]));
yy.addEntity(tv(d[4]));
yy.addRelationship(tv(d[0]), d[8], tv(d[4]), d[2])
}
%}
| entityName _ "{" _ attributes _ "}"
| entityName _ "{" _ attributes _ "}" %WS:* %NL
{%
function(d) {
yy.addEntity(tv(d[0]));
Expand All @@ -70,18 +70,19 @@ statement ->
%}
| entityName "{":? "}":? {% (d) => yy.addEntity(tv(d[0])) %}
| entityName {% (d) => yy.addEntity(tv(d[0])) %}
| paramClause _ %NL {%
| paramClause %WS:* %NL {%
function(d) {
const { type, ...styleParam } = d[0]
yy.addParam(styleParam)
}
%}
| configClause _ %NL {%
| configClause %WS:* %NL {%
function(d) {
yy.addOverrideConfig(d[0])
}
%}
| comment _ %NL
| %NL

entityName ->
%VALID_TEXT {% id %}
Expand All @@ -93,12 +94,12 @@ attributes ->
%}

attribute ->
attributeType __ attributeName __ %VALID_TEXT _ %NL {%
attributeType __ attributeName __ %VALID_TEXT %WS:* %NL {%
function(d) {
return { attributeType: tv(d[0]), attributeName: tv(d[2]), attributeKey: tv(d[4]) }
}
%}
| attributeType __ attributeName _ %NL {% (d) => { return { attributeType: tv(d[0]), attributeName: tv(d[2]) } } %}
| attributeType __ attributeName %WS:* %NL {% (d) => { return { attributeType: tv(d[0]), attributeName: tv(d[2]) } } %}

attributeType -> %VALID_TEXT {% id %}

Expand Down
4 changes: 2 additions & 2 deletions packages/pintora-diagrams/src/mindmap/parser/mindmap.ne
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const COMMON_TOKEN_RULES = {
let lexer = moo.states({
main: {
NL: MOO_NEWLINE,
WS: { match: /[ ]+/, lineBreaks: false },
WS: { match: / +/, lineBreaks: false },
ASTERISKS: /\*+/,
PLUS: /\++/,
MINUS: /\-+/,
Expand Down Expand Up @@ -86,7 +86,7 @@ statement ->
}
%}
| paramClause _ %NL
| configOpenCloseClause %NL
| configOpenCloseClause _ %NL
| comment _ %NL

levelNotation ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('sequence parser', () => {
it('can parse unicode chars', () => {
const backquoteExample = stripStartEmptyLines(`
sequenceDiagram
autonumber
autonumber
用户 ->> Pintora: 帮我画张时序图
activate Pintora
alt DSL 正确
Expand Down Expand Up @@ -106,10 +106,10 @@ sequenceDiagram

it('can parse participant', () => {
const example = stripStartEmptyLines(`
sequenceDiagram
sequenceDiagram
participant A as "Alice"
participant B as "Bob"
participant C
participant B as "Bob"
participant C
A-->B: hello
A-->C: yoho
`)
Expand Down
12 changes: 6 additions & 6 deletions packages/pintora-diagrams/src/sequence/parser/sequenceDiagram.ne
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ export function setYY(v) {
%}

start -> __ start {% (d) => d[1] %}
| "sequenceDiagram" document __:? {%
| "sequenceDiagram" _ document __:? {%
function(d) {
// console.log('[sequenceDiagram]', JSON.stringify(d, null, 2))
return d[1]
return d[2]
}
%}

Expand Down Expand Up @@ -102,21 +102,21 @@ statement ->
return d[2]
}
%}
| participantWord __ classifiableActor %NL {%
| participantWord __ classifiableActor %WS:? %NL {%
function(d) {
return d[2]
}
%}
| signal %NL {% id %}
| "autonumber" %NL {% (d) => yy.enableSequenceNumbers() %}
| "activate" _ actor %NL {%
| "autonumber" %WS:? %NL {% (d) => yy.enableSequenceNumbers() %}
| "activate" _ actor %WS:? %NL {%
function(d) {
return {
type: 'activeStart', signalType: yy.LINETYPE.ACTIVE_START, actor: d[2]
}
}
%}
| "deactivate" _ actor %NL {%
| "deactivate" _ actor %WS:? %NL {%
function(d) {
return {
type: 'activeEnd', signalType: yy.LINETYPE.ACTIVE_END, actor: d[2]
Expand Down

0 comments on commit ff1ef0c

Please sign in to comment.