Skip to content

Commit

Permalink
parser: setMinIndent for if that are not part of an else if
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellilltokiwa committed Jun 3, 2024
1 parent 78aade2 commit 9b6f76d
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 45 deletions.
9 changes: 6 additions & 3 deletions lib/container/ps_map.fz
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ O(n log n).
at0 := (at
+ (if (size & sz0) != 0 nt else 0)
+ (if (size & sz ) != 0 sz else 0))
m0 := if (size & sz) != 0
m0 :=
if (size & sz) != 0
min0 m at at+sz-1
else
m
Expand Down Expand Up @@ -297,7 +298,8 @@ O(n log n).
at0 := (at
+ (if (size & sz0) != 0 nt else 0)
+ (if (size & sz ) != 0 sz else 0))
m0 := if (size & sz) != 0
m0 :=
if (size & sz) != 0
max0 m at at+sz-1
else
m
Expand Down Expand Up @@ -342,7 +344,8 @@ O(n log n).
at0 := (at
+ (if (size & sz0) != 0 nt else 0)
+ (if (size & sz ) != 0 sz else 0))
a0 := if (size & sz) != 0
a0 :=
if (size & sz) != 0
add_all a at at+sz-1
else
a
Expand Down
10 changes: 5 additions & 5 deletions lib/mutate/array.fz
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ is
=>
check_and_replace
d := if (data.length.as_i64 > length) data
else
new_data := fuzion.sys.internal_array_init T (max 8 data.length*2)
for i in indices do
new_data[i.as_i32] := data[i.as_i32]
new_data
else
new_data := fuzion.sys.internal_array_init T (max 8 data.length*2)
for i in indices do
new_data[i.as_i32] := data[i.as_i32]
new_data
d[length.as_i32] := o
set data := d
set length := length+1
Expand Down
17 changes: 17 additions & 0 deletions src/dev/flang/parser/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,12 @@ else if (x.equals("-stringLiteralEscapes"))
private int _tokenPos = -1;


/**
* The previous token that was not ignored.
*/
private Token _lastToken = Token.t_undefined;


/**
* Position of the previous token, -1 if none
*/
Expand Down Expand Up @@ -691,6 +697,7 @@ public Lexer(Path fileName, byte[] sf)
_curToken = original._curToken;
_curLiteral = original._curLiteral;
_tokenPos = original._tokenPos;
_lastToken = original._lastToken;
_lastTokenPos = original._lastTokenPos;
_lastTokenEndPos = original._lastTokenEndPos;
_minIndent = original._minIndent;
Expand Down Expand Up @@ -1032,6 +1039,7 @@ boolean skipBracketTermWithNLs(Parens brackets, Callable<Boolean> c)
*/
public void next()
{
_lastToken = _curToken;
_lastTokenPos = _tokenPos;
_lastTokenEndPos = tokenEndPos();
_ignoredTokenBefore = false;
Expand Down Expand Up @@ -1131,6 +1139,15 @@ public int tokenPos()
}


/**
* The previous non-skip token in the source file.
*/
Token lastToken()
{
return _lastToken;
}


/**
* The byte position of the previous non-skip token in the source file. -1 if
* this does not exist.
Expand Down
3 changes: 3 additions & 0 deletions src/dev/flang/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2794,10 +2794,13 @@ If ifexpr()
{
return relaxLineAndSpaceLimit(() -> {
SourcePosition pos = tokenSourcePos();
// set new min indent unless this `if` is part of an `else if`
var old = lastToken() == Token.t_else ? null : setMinIndent(tokenPos());
match(Token.t_if, "ifexpr");
Expr e = exprInLine();
Block b = thenPart(false);
var els = elseBlock();
if (old != null) { setMinIndent(old); }
return new If(pos, e, b,
// do no use empty blocks as else blocks since the source position
// of those block might be somewhere unexpected.
Expand Down
8 changes: 4 additions & 4 deletions tests/floating_point_numbers/floating_point_numbers.fz
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ floating_point_numbers is

chck(b bool, msg String) =>
s := if b
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
say (s + msg)


Expand Down
10 changes: 5 additions & 5 deletions tests/generics/genericstest.fz
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ genericstest is

chck(b bool, msg String) =>
s := if b
"PASSED: "
else
set_exit_code 1 ? unit =>
| error =>
"FAILED: "
"PASSED: "
else
set_exit_code 1 ? unit =>
| error =>
"FAILED: "
say (s + msg)

List(T type) is
Expand Down
8 changes: 4 additions & 4 deletions tests/if_negative/iftest_negative.fz
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ iftest_negative is

chck(b bool, msg String) is
s := if b
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
say (s + msg)

testIfNonBoolean1 is
Expand Down
8 changes: 4 additions & 4 deletions tests/loop/looptest.fz
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ looptest is

chck(b bool, msg String) =>
s := if b
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
say (s + msg)

a := array i32 100 (i -> if (i % 23 = 0) then -2*i else i*i)
Expand Down
8 changes: 4 additions & 4 deletions tests/redef_args/redef_args.fz
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ redef_args is

chck(b bool, msg String) =>
s := if (b)
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
say (s + msg)

RComplex(RT type) ref is
Expand Down
8 changes: 4 additions & 4 deletions tests/reg_issue1158_type_of_type_parameters/test_type_of.fz
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ test_type_of is

chck(b bool, msg String) =>
s := if b
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
say (s + msg)

chck_cmp(s1, s2 String) =>
Expand Down
25 changes: 25 additions & 0 deletions tests/reg_issue2691/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation. If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
# Tokiwa Software GmbH, Germany
#
# Source code of Fuzion test Makefile
#
# -----------------------------------------------------------------------

override NAME = reg_issue2691
include ../simple.mk
39 changes: 39 additions & 0 deletions tests/reg_issue2691/reg_issue2691.fz
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation. If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
# Tokiwa Software GmbH, Germany
#
# Source code of Fuzion test
#
# -----------------------------------------------------------------------

reg_issue2691 is

test0 =>
for i in 1..10
do if i = 2 then say "B: found 2"
else
say "B: done"
_ := test0

test1 =>
if true
then if false then
else
panic "*** unreachable ***"

_ := test1
Empty file.
2 changes: 2 additions & 0 deletions tests/reg_issue2691/reg_issue2691.fz.expected_out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
B: found 2
B: done
8 changes: 4 additions & 4 deletions tests/strings/stringstest.fz
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ stringstest is

chck(b bool, msg String) =>
s := if b
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
say (s + msg)

x := 42
Expand Down
8 changes: 4 additions & 4 deletions tests/typeinference/typeinference.fz
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ typeinference is

chck(b bool, msg String) =>
s := if b
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
say (s + msg)

typeInferencingFromIf1(b bool) is
Expand Down
8 changes: 4 additions & 4 deletions tests/unicode/unicodetest.fz
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ unicodetest is

chck(b bool, msg String) =>
s := if b
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
"PASSED: "
else
_ := set_exit_code 1
"FAILED: "
say (s + msg)


Expand Down

0 comments on commit 9b6f76d

Please sign in to comment.