Skip to content

Commit

Permalink
Sequence diagram: condition. #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Jun 25, 2021
1 parent 58f189d commit ad16bd3
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 47 deletions.
58 changes: 27 additions & 31 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,33 @@

```dial
title: Foo Bar Baz
foo.title: Lorem ipsum
foo.type: actor
foo -> bar
bar -> baz: int func(a, b)
bar -> baz: (a, b) func(a, b)
bar -> qux: func(a)
# This is comment
@over:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore.
@over bar: note
@over bar~qux: note
@over bar~: note
@over ~bar: note
@left of bar: note
@right of qux: note
qux -> quux: func(a)
for i in list
qux -> quux: func(a)
if condition
qux -> quux: int func(a)
elif condition
qux -> quux
else
qux -> bar
+ title: Foo Bar Baz
+
+ foo.title: Lorem ipsum
+ foo.type: actor
+
+ foo -> bar
+ bar -> baz: int func(a, b)
+ bar -> baz: (a, b) func(a, b)
+ bar -> qux: func(a)
+ # This is comment
- @over:
- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
- tempor incididunt ut labore et dolore.
- @over bar: note
- @over bar~qux: note
- @over bar~: note
- @over ~bar: note
- @left of bar: note
- @right of qux: note
+ for i in list
+ qux -> quux: func(a)
+ if condition
+ qux -> quux: int func(a)
+ elif condition
+ qux -> quux
+ else
+ qux -> bar
```

Expand Down
74 changes: 59 additions & 15 deletions dial/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, title, type_='module'):
self.type = type_


class Item(Visible, Interpreter, list, metaclass=abc.ABCMeta):
class Item(Visible, Interpreter, metaclass=abc.ABCMeta):
text = None

def __init__(self, tokenizer):
Expand All @@ -32,6 +32,14 @@ def __repr__(self):
if self.text:
result += f': {self.text}'

return result


class Container(Item, list):

def __repr__(self):
result = super().__repr__()

if len(self):
result += '\n'
for c in self:
Expand All @@ -41,7 +49,7 @@ def __repr__(self):
return result.rstrip('\n')


class Call(Item):
class Call(Container):
caller = None
callee = None

Expand All @@ -67,7 +75,33 @@ def _complete(self, caller, callee, text=None):
}


class Loop(Item):
class Loop(Container):
type_ = None

@property
def _short_repr(self):
return self.type_

def _complete(self, type_, text=None):
self.type_ = type_
super()._complete(text)

statemap = {
'start': {
NAME: Goto(nextstate='name'),
},
'name': {
NAME: Goto(nextstate='name'),
NEWLINE: FinalConsume(_complete),
COLON: Goto(nextstate=':'),
},
':': {EVERYTHING: {
NEWLINE: FinalConsume(_complete)
}}
}


class Condition(Container):
type_ = None

@property
Expand Down Expand Up @@ -151,6 +185,9 @@ def _new_call(self, call):
def _new_loop(self, loop):
self.current.append(loop)

def _new_condition(self, condition):
self.current.append(condition)

def _attr(self, attr, value):
value = value.strip()

Expand All @@ -166,34 +203,41 @@ def _module_attr(self, module, attr, value):

setattr(self.modules[module], attr, value.strip())

_keywords = {
'for': New(Loop, callback=_new_loop, nextstate='start'),
'while': New(Loop, callback=_new_loop, nextstate='start'),
'loop': New(Loop, callback=_new_loop, nextstate='start'),
'if': New(Condition, callback=_new_condition, nextstate='start'),
'alt': New(Condition, callback=_new_condition, nextstate='start'),
'elif': New(Condition, callback=_new_condition, nextstate='start'),
'else': New(Condition, callback=_new_condition, nextstate='start'),
}

statemap = {
'start': {
HASH: {EVERYTHING: {NEWLINE: Ignore(nextstate='start')}},
NEWLINE: Ignore(nextstate='start'),
INDENT: {
NAME: Goto(callback=_indent, nextstate=' name'),
},
INDENT: Goto(callback=_indent, nextstate='indent'),
DEDENT: Ignore(callback=_dedent, nextstate='start'),
EOF: Ignore(nextstate='start'),
NAME: Switch(
for_=New(Loop, callback=_new_loop, nextstate='start'),
while_=New(Loop, callback=_new_loop, nextstate='start'),
loop=New(Loop, callback=_new_loop, nextstate='start'),
default=Goto(nextstate='name')
),
NAME: Switch(default=Goto(nextstate='name'), **_keywords)
},
'indent': {
HASH: {EVERYTHING: {NEWLINE: Ignore(nextstate='start')}},
NAME: Switch(default=Goto(nextstate=' name'), **_keywords)
},
'name': {
RARROW: New(Call, callback=_new_call, nextstate='start'),
COLON: Goto(nextstate='attr:'),
DOT: {NAME: {COLON: Goto(nextstate='mod.attr:')}},
},
' name': {
RARROW: New(Call, callback=_new_call, nextstate='start')
},
'attr:': {
EVERYTHING: {NEWLINE: Consume(_attr, nextstate='start')}
},
'mod.attr:': {
EVERYTHING: {NEWLINE: Consume(_module_attr, nextstate='start')}
},
' name': {
RARROW: New(Call, callback=_new_call, nextstate='start')
}
}
42 changes: 41 additions & 1 deletion tests/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,52 @@
from dial.interpreter import BadSyntax, BadAttribute


# def test_sequence_note():
# d = SequenceDiagram(Tokenizer())
# s = '''# Sequence
# title: note
# @note: Foo Bar baz
# '''
# d.parse(s)
# assert repr(d) == s[:-1]


def test_sequence_condition():
d = SequenceDiagram(Tokenizer())
s = '''# Sequence
title: condition
if: foo > 1
foo -> bar: baz(*)
elif: foo < 0
else
alt: j > 9
foo -> bar: baz(*)
else
foo -> bar: baz(*)
'''
d.parse(s)
assert repr(d) == s[:-1]


def test_sequence_loop():
d = SequenceDiagram(Tokenizer())
s = '''# Sequence
title: loop
foo -> bar
loop: over list
bar -> baz
for
foo -> bar: baz(*)
for: i in range(10)
foo -> bar: baz(i)
while: j > 0
foo -> bar: baz(j)
loop: over [1, 2, 3]
for: i in list(7...99)
while: bool
foo -> thud: wow
'''
d.parse(s)
assert repr(d) == s[:-1]
Expand All @@ -25,11 +59,17 @@ def test_sequence_loop():
def test_sequence_comment():
d = SequenceDiagram(Tokenizer())
s = '''# Sequence
title: Comment
# This is comment
foo -> bar:
# This is comment too
'''
d.parse(s)
assert repr(d) == '''# Sequence
title: Untitled'''
title: Comment
foo -> bar'''


def test_sequence_moduleattr_error():
Expand Down

0 comments on commit ad16bd3

Please sign in to comment.