Skip to content

Commit

Permalink
Add more flexible static parse method (#6306)
Browse files Browse the repository at this point in the history
* Add more flexible static parse method

* Remove extraneous new line

* Add SkriptPattern overload

---------

Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
  • Loading branch information
Pikachu920 and sovdeeth committed Apr 3, 2024
1 parent dfb5ffd commit e454a10
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/ch/njol/skript/lang/SkriptParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,26 @@ public static ParseResult parse(String text, String pattern) {
return new SkriptParser(text, PARSE_LITERALS, ParseContext.COMMAND).parse_i(pattern);
}

/**
* Parses the text as the given pattern with the given parse context and parse flags.
* <p>
* Prints parse errors (i.e. must start a ParseLog before calling this method)
*/
@Nullable
public static ParseResult parse(String text, String pattern, int parseFlags, ParseContext parseContext) {
return new SkriptParser(text, parseFlags, parseContext).parse_i(pattern);
}

/**
* Parses the text as the given pattern with the given parse context and parse flags.
* <p>
* Prints parse errors (i.e. must start a ParseLog before calling this method)
*/
@Nullable
public static ParseResult parse(String text, SkriptPattern pattern, int parseFlags, ParseContext parseContext) {
return parse(text, pattern.toString(), parseFlags, parseContext);
}

/**
* Finds the closing bracket of the group at <tt>start</tt> (i.e. <tt>start</tt> has to be <i>in</i> a group).
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* This file is part of Skript.
*
* Skript 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, either version 3 of the License, or
* (at your option) any later version.
*
* Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package org.skriptlang.skript.test.tests.parsing;

import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.patterns.PatternCompiler;
import ch.njol.skript.patterns.SkriptPattern;
import org.junit.Assert;
import org.junit.Test;

import java.util.stream.Stream;

public class StaticParseTest {

private Stream<ParseResult> computeParseResults(String text, String pattern) {
ParseResult stringParseResult = SkriptParser.parse(text, pattern, SkriptParser.ALL_FLAGS, ParseContext.COMMAND);
SkriptPattern compiledPattern = PatternCompiler.compile(pattern);
ParseResult compiledParseResult = SkriptParser.parse(text, compiledPattern, SkriptParser.ALL_FLAGS, ParseContext.COMMAND);
return Stream.of(stringParseResult, compiledParseResult);
}

@Test
public void testMultipleSingleExpressions() {
computeParseResults("1 test 2 test 3", "%number% test %number% test %number%").forEach(result -> {
Assert.assertNotNull("parse method returned null", result);
Assert.assertEquals("parse method returned wrong number of expressions", 3, result.exprs.length);
Assert.assertEquals("parse method returned wrong first expression", 1L, result.exprs[0].getSingle(null));
Assert.assertEquals("parse method returned wrong second expression", 2L, result.exprs[1].getSingle(null));
Assert.assertEquals("parse method returned wrong first expression", 3L, result.exprs[2].getSingle(null));
});
}

@Test
public void testMultipleMultipleExpressions() {
computeParseResults("1, 2 and 3 test 4, 5 and 6", "%numbers% test %numbers%").forEach(result -> {
Assert.assertNotNull("parse method returned null", result);
Assert.assertEquals("parse method returned wrong number of expressions", 2, result.exprs.length);
Assert.assertArrayEquals("parse method returned wrong first expression", new Long[]{1L,2L,3L}, result.exprs[0].getArray(null));
Assert.assertArrayEquals("parse method returned wrong second expression", new Long[]{4L,5L,6L}, result.exprs[1].getArray(null));
});
}

@Test
public void testMultipleMixedExpressions() {
computeParseResults("1 test 2, 3 and 4", "%number% test %numbers%").forEach(result -> {
Assert.assertNotNull("parse method returned null", result);
Assert.assertEquals("parse method returned wrong number of expressions", 2, result.exprs.length);
Assert.assertEquals("parse method returned wrong first expression", 1L, result.exprs[0].getSingle(null));
Assert.assertArrayEquals("parse method returned wrong second expression", new Long[]{2L,3L,4L}, result.exprs[1].getArray(null));
});
}

@Test
public void testMultipleExpression() {
computeParseResults("1, 2 and 3", "%numbers%").forEach(result -> {
Assert.assertNotNull("parse method returned null", result);
Assert.assertEquals("parse method returned wrong number of expressions", 1, result.exprs.length);
Assert.assertArrayEquals("parse method returned wrong first expression", new Long[]{1L,2L,3L}, result.exprs[0].getArray(null));
});

}

@Test
public void testSingleExpression() {
computeParseResults("1", "%number%").forEach(result -> {
Assert.assertNotNull("parse method returned null", result);
Assert.assertEquals("parse method returned wrong number of expressions", 1, result.exprs.length);
Assert.assertEquals("parse method returned wrong first expression", 1L, result.exprs[0].getSingle(null));
});
}

}

0 comments on commit e454a10

Please sign in to comment.