Skip to content

Commit

Permalink
v1.0.2 #3
Browse files Browse the repository at this point in the history
Fix logical diverts
  • Loading branch information
viniciusgerevini authored Jun 4, 2021
2 parents 83804e4 + 9218217 commit f1b6694
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 6 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)

The Working in Progress (WIP) section is for changes that are already in master, but haven't been published to Godot's asset library yet. Even though the section is called WIP, all changes in master are stable and functional.
## 1.0.2 (2021-06-04)

### Added

- Condition blocks before line can use the "when" keyword.

### Fixed

- Diverts support conditional blocks before and after line

## 1.0.1 (2021-05-09)

Expand Down
15 changes: 13 additions & 2 deletions addons/clyde/parser/Lexer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,27 @@ func _handle_divert():
_position += 1
_column += 1

return Token(TOKEN_DIVERT, _line, initial_column, _array_join(values).strip_edges())
var token = Token(TOKEN_DIVERT, _line, initial_column, _array_join(values).strip_edges())

var linebreak = _get_following_line_break()
if linebreak:
return [ token, linebreak ]

return token


func _handle_divert_parent():
var initial_column = _column
_position += 2
_column += 2

return Token(TOKEN_DIVERT_PARENT, _line, initial_column)
var token = Token(TOKEN_DIVERT_PARENT, _line, initial_column)

var linebreak = _get_following_line_break()
if linebreak:
return [ token, linebreak ]

return token

func _handle_start_variations():
var initial_column = _column
Expand Down
21 changes: 19 additions & 2 deletions addons/clyde/parser/Parser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func _lines():
if _tokens.peek([Lexer.TOKEN_KEYWORD_SET, Lexer.TOKEN_KEYWORD_TRIGGER]):
lines = [_line_with_action()]
else:
if _tokens.peek([Lexer.TOKEN_KEYWORD_WHEN]):
_tokens.consume([Lexer.TOKEN_KEYWORD_WHEN])
lines = [_conditional_line()]


Expand Down Expand Up @@ -358,11 +360,26 @@ func _divert():
_tokens.consume([ Lexer.TOKEN_DIVERT, Lexer.TOKEN_DIVERT_PARENT ])
var divert = _tokens.current_token

var token
match divert.token:
Lexer.TOKEN_DIVERT:
return DivertNode(divert.value)
token = DivertNode(divert.value)
Lexer.TOKEN_DIVERT_PARENT:
return DivertNode('<parent>')
token = DivertNode('<parent>')

if _tokens.peek([Lexer.TOKEN_LINE_BREAK]):
_tokens.consume([Lexer.TOKEN_LINE_BREAK])
return token

if _tokens.peek([Lexer.TOKEN_EOF]):
return token

if _tokens.peek([Lexer.TOKEN_BRACE_OPEN]):
_tokens.consume([Lexer.TOKEN_BRACE_OPEN])
token = _line_with_action(token)


return token


func _variations():
Expand Down
2 changes: 1 addition & 1 deletion addons/clyde/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="Clyde Dialogue"
description="Interpreter and importer for Clyde Dialogue Language"
author="Vinicius Gerevini"
version="1.0.1"
version="1.0.2"
script="plugin.gd"
23 changes: 23 additions & 0 deletions test/test_lexer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,38 @@ hello
assert_eq_deep(tokens, [
{ "token": Lexer.TOKEN_TEXT, "value": 'hello', "line": 1, "column": 0, },
{ "token": Lexer.TOKEN_DIVERT, "value": 'first divert', "line": 2, "column": 0, },
{ "token": Lexer.TOKEN_LINE_BREAK, "value": null, "line": 2, "column": 15, },
{ "token": Lexer.TOKEN_OPTION, "line": 4, "column": 0, "value": null },
{ "token": Lexer.TOKEN_TEXT, "value": 'test', "line": 4, "column": 2 },
{ "token": Lexer.TOKEN_INDENT, "line": 5, "column": 0, "value": null },
{ "token": Lexer.TOKEN_DIVERT, "value": 'divert', "line": 5, "column": 2 },
{ "token": Lexer.TOKEN_LINE_BREAK, "value": null, "line": 5, "column": 11, },
{ "token": Lexer.TOKEN_DIVERT_PARENT, "line": 6, "column": 2, "value": null },
{ "token": Lexer.TOKEN_LINE_BREAK, "value": null, "line": 6, "column": 4, },
{ "token": Lexer.TOKEN_DIVERT, "value": 'END', "line": 7, "column": 2 },
{ "token": Lexer.TOKEN_LINE_BREAK, "value": null, "line": 7, "column": 8, },
{ "token": Lexer.TOKEN_EOF, "line": 8, "column": 0, "value": null},
])


func test_divert_on_eof():
var lexer = Lexer.new()
var tokens = lexer.init("-> div").get_all()
assert_eq_deep(tokens, [
{ "token": Lexer.TOKEN_DIVERT, "value": 'div', "line": 0, "column": 0, },
{ "token": Lexer.TOKEN_EOF, "line": 0, "column": 6, "value": null},
])


func test_divert_parent_on_eof():
var lexer = Lexer.new()
var tokens = lexer.init("<-").get_all()
assert_eq_deep(tokens, [
{ "token": Lexer.TOKEN_DIVERT_PARENT, "value": null, "line": 0, "column": 0, },
{ "token": Lexer.TOKEN_EOF, "line": 0, "column": 2, "value": null},
])


func test_variations():
var lexer = Lexer.new()
var tokens = lexer.init("""
Expand Down Expand Up @@ -390,6 +412,7 @@ func test_variations():
{ "token": Lexer.TOKEN_INDENT, "line": 7, "column": 0, "value": null },
{ "token": Lexer.TOKEN_MINUS, "line": 7, "column": 2, "value": null },
{ "token": Lexer.TOKEN_DIVERT, "value": 'nope', "line": 7, "column": 4 },
{ "token": Lexer.TOKEN_LINE_BREAK, "value": null, "line": 7, "column": 11, },
{ "token": Lexer.TOKEN_MINUS, "line": 8, "column": 2, "value": null, },
{ "token": Lexer.TOKEN_TEXT, "value": 'yep', "line": 8, "column": 4 },
{ "token": Lexer.TOKEN_DEDENT, "line": 9, "column": 0, "value": null },
Expand Down
42 changes: 42 additions & 0 deletions test/test_parser_logic.gd
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,17 @@ func test_string_literal():
])
assert_eq_deep(result, expected)

func test_condition_before_line_with_keyword():
var result = parse("{ when some_var } This is conditional")
var expected = _create_doc_payload([
{
"type": "conditional_content",
"conditions": { "type": "variable", "name": "some_var" },
"content": { "type": "line", "value": "This is conditional", "speaker": null, "id": null, "tags": null, }
},
])
assert_eq_deep(result, expected)


func test_condition_after_line():
var result = parse("This is conditional { when some_var }")
Expand Down Expand Up @@ -416,6 +427,18 @@ func test_conditional_divert():
assert_eq_deep(result, expected)


func test_conditional_divert_after():
var result = parse("-> some_block { some_var }")
var expected = _create_doc_payload([
{
"type": "conditional_content",
"conditions": { "type": "variable", "name": "some_var" },
"content": { "type": "divert", "target": "some_block", }
},
])
assert_eq_deep(result, expected)


func test_conditional_option():
var result = parse("""
* { some_var } option 1
Expand Down Expand Up @@ -864,6 +887,25 @@ func test_options_assignment():
assert_eq_deep(result, expected)


func test_divert_with_assignment():
var result = parse("-> go { set a = 2 }")
var expected = _create_doc_payload([{
"type": 'action_content',
"action": {
"type": 'assignments',
"assignments": [
{
"type": 'assignment',
"variable": { "type": 'variable', "name": 'a', },
"operation": 'assign',
"value": { "type": 'literal', "name": 'number', "value": 2.0, },
},
],
},
"content": { "type": 'divert', "target": 'go' },
}])
assert_eq_deep(result, expected)


func test_trigger_event():
var result = parse("{ trigger some_event } trigger")
Expand Down

0 comments on commit f1b6694

Please sign in to comment.