Skip to content

Commit

Permalink
Fixed token merge/substring column
Browse files Browse the repository at this point in the history
  • Loading branch information
maccasoft committed Nov 19, 2023
1 parent 56affa5 commit b4df9c5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2021-23 Marco Maccaferri and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License v1.0 which accompanies this
* distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package com.maccasoft.propeller.model;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.maccasoft.propeller.spin1.Spin1TokenStream;

class TokenTest {

@Test
void testMerge() {
Spin1TokenStream stream = new Spin1TokenStream("\n @a");
stream.nextToken(); // NL
Token token1 = stream.nextToken(); // @
Token token2 = stream.nextToken(); // a
Token result = token1.merge(token2);
Assertions.assertEquals(token1.column, result.column);
Assertions.assertEquals(token1.line, result.line);
Assertions.assertEquals(token1.start, result.start);
Assertions.assertEquals(token2.stop, result.stop);
}

@Test
void testSubstringStart() {
Spin1TokenStream stream = new Spin1TokenStream("\n prefix.suffix");
stream.nextToken(); // NL
Token token = stream.nextToken();
Assertions.assertEquals("prefix.suffix", token.getText());

Token result = token.substring(6);
Assertions.assertEquals(".suffix", result.getText());
Assertions.assertEquals(token.column + 6, result.column);
Assertions.assertEquals(token.line, result.line);
Assertions.assertEquals(token.start + 6, result.start);
Assertions.assertEquals(token.stop, result.stop);
}

@Test
void testSubstringStartStop() {
Spin1TokenStream stream = new Spin1TokenStream("\n prefix.middle.suffix");
stream.nextToken(); // NL
Token token = stream.nextToken();
Assertions.assertEquals("prefix.middle.suffix", token.getText());

Token result = token.substring(6, 12);
Assertions.assertEquals(".middle", result.getText());
Assertions.assertEquals(token.column + 6, result.column);
Assertions.assertEquals(token.line, result.line);
Assertions.assertEquals(token.start + 6, result.start);
Assertions.assertEquals(token.start + 12, result.stop);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public Token merge(Token token) {
Token result = new Token();
result.stream = stream;
result.line = line;
result.column = column;
result.start = start < token.start ? start : token.start;
result.stop = stop > token.stop ? stop : token.stop;
result.type = type == token.type ? type : 0;
Expand All @@ -91,6 +92,7 @@ public Token substring(int start) {
Token result = new Token();
result.stream = stream;
result.line = line;
result.column = column + start;
result.start = this.start + start;
result.stop = this.stop;
result.type = this.type;
Expand All @@ -101,6 +103,7 @@ public Token substring(int start, int stop) {
Token result = new Token();
result.stream = stream;
result.line = line;
result.column = column + start;
result.start = this.start + start;
result.stop = this.start + stop;
result.type = this.type;
Expand Down

0 comments on commit b4df9c5

Please sign in to comment.