Skip to content

Commit 9e19573

Browse files
committed
Pattern improvements and syntax sections.
1 parent bf545d1 commit 9e19573

File tree

18 files changed

+545
-29
lines changed

18 files changed

+545
-29
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
<dependency>
114114
<groupId>mx.kenzie</groupId>
115115
<artifactId>foundation</artifactId>
116-
<version>1.1.2</version>
116+
<version>1.1.3</version>
117117
<scope>compile</scope>
118118
</dependency>
119119
<dependency>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2021 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.api.syntax;
8+
9+
import mx.kenzie.foundation.compiler.State;
10+
import org.byteskript.skript.api.LanguageElement;
11+
import org.byteskript.skript.api.Library;
12+
import org.byteskript.skript.compiler.CompileState;
13+
import org.byteskript.skript.compiler.Context;
14+
15+
public abstract class SectionEntry extends Section {
16+
public SectionEntry(Library provider, LanguageElement type, String... patterns) {
17+
super(provider, type, patterns);
18+
}
19+
20+
@Override
21+
public boolean allowedIn(State state, Context context) {
22+
return state == CompileState.MEMBER_BODY;
23+
}
24+
25+
@Override
26+
public CompileState getSubState() {
27+
return CompileState.MEMBER_BODY;
28+
}
29+
}

src/main/java/org/byteskript/skript/api/syntax/SimpleEntry.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package org.byteskript.skript.api.syntax;
88

9+
import mx.kenzie.foundation.Type;
910
import mx.kenzie.foundation.compiler.State;
1011
import org.byteskript.skript.api.LanguageElement;
1112
import org.byteskript.skript.api.Library;
@@ -18,6 +19,11 @@ public SimpleEntry(Library provider, LanguageElement type, String... patterns) {
1819
super(provider, type, patterns);
1920
}
2021

22+
@Override
23+
public boolean allowAsInputFor(Type type) {
24+
return false;
25+
}
26+
2127
@Override
2228
public Pattern.Match match(String thing, Context context) {
2329
return super.match(thing, context);

src/main/java/org/byteskript/skript/compiler/AreaFlag.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.byteskript.skript.api.Flag;
1010

1111
public enum AreaFlag implements Flag {
12+
IN_SYNTAX,
1213
IN_FUNCTION,
1314
IN_TYPE,
1415
IN_ABSTRACT_TYPE,

src/main/java/org/byteskript/skript/compiler/Pattern.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,15 @@ public java.util.regex.Pattern[] getCompiledPatterns() {
140140
}
141141

142142
public Match match(final String thing, final Context context) {
143-
int found = 0;
144-
for (java.util.regex.Pattern pattern : patternMap.keySet()) {
145-
final Matcher matcher = pattern.matcher(thing);
146-
if (matcher.find()) return new Match(matcher, found, convert(context, patternMap.get(pattern)));
147-
found++;
148-
}
149-
return null;
143+
return match(thing, context, null);
150144
}
151145

152146
public Match match(final String thing, final Context context, final Object meta) {
147+
int found = 0;
153148
for (java.util.regex.Pattern pattern : patternMap.keySet()) {
154149
final Matcher matcher = pattern.matcher(thing);
155-
if (matcher.find()) return new Match(matcher, meta, convert(context, patternMap.get(pattern)));
150+
if (matcher.find()) return new Match(matcher, found, meta != null ? meta : found, convert(context, patternMap.get(pattern)));
151+
found++;
156152
}
157153
return null;
158154
}
@@ -178,18 +174,22 @@ public static final class Match {
178174
private final Object meta;
179175
private final Type[] expected;
180176
private final String[] groups;
177+
public final int matchedPattern;
181178

182179
public Match(Matcher matcher, Object meta, Type... expected) {
180+
this(matcher, 0, meta, expected);
181+
}
182+
183+
public Match(Matcher matcher, int matchedPattern, Object meta, Type... expected) {
183184
this.matcher = matcher;
184185
this.meta = meta;
185186
this.expected = expected;
186-
{
187-
final List<String> list = new ArrayList<>();
188-
for (int i = 1; i <= matcher.groupCount(); i++) {
189-
list.add(matcher.group(i).trim());
190-
}
191-
this.groups = list.toArray(new String[0]);
187+
this.matchedPattern = matchedPattern;
188+
final List<String> list = new ArrayList<>();
189+
for (int i = 1; i <= matcher.groupCount(); i++) {
190+
list.add(matcher.group(i).trim());
192191
}
192+
this.groups = list.toArray(new String[0]);
193193
}
194194

195195
public Match(Matcher matcher, Type... expected) {

src/main/java/org/byteskript/skript/compiler/SkriptLangSpec.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.byteskript.skript.lang.syntax.control.RemoveEffect;
2626
import org.byteskript.skript.lang.syntax.control.SetEffect;
2727
import org.byteskript.skript.lang.syntax.entry.*;
28+
import org.byteskript.skript.lang.syntax.entry.syntax.*;
2829
import org.byteskript.skript.lang.syntax.event.AnyLoadEvent;
2930
import org.byteskript.skript.lang.syntax.event.CurrentEventExpression;
3031
import org.byteskript.skript.lang.syntax.event.LoadEvent;
@@ -122,7 +123,12 @@ private SkriptLangSpec() {
122123
registerSyntax(CompileState.MEMBER_BODY,
123124
new Verify(),
124125
new Trigger(),
125-
new ReturnType()
126+
new ReturnType(),
127+
new SyntaxEntry(),
128+
new EffectEntry(),
129+
new ExpressionEntry(),
130+
new PropertyEntry(),
131+
new ModeEntry()
126132
);
127133
registerSyntax(CompileState.CODE_BODY,
128134
new PrintEffect(),
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2021 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.compiler.structure;
8+
9+
import org.byteskript.skript.api.HandlerType;
10+
import org.byteskript.skript.api.SyntaxElement;
11+
import org.byteskript.skript.api.note.Effect;
12+
import org.byteskript.skript.api.note.Expression;
13+
import org.byteskript.skript.api.note.Property;
14+
import org.byteskript.skript.compiler.Context;
15+
import org.byteskript.skript.lang.syntax.entry.syntax.ICreateSyntax;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
public class SyntaxTree extends ProgrammaticSplitTree {
21+
22+
private final SectionMeta owner;
23+
private final List<Handler> handlers;
24+
public HandlerType mode;
25+
26+
public record Handler(Type type, String pattern) {
27+
}
28+
29+
public enum Type {
30+
EFFECT(Effect.class), EXPRESSION(Expression.class), PROPERTY(Property.class);
31+
public final Class<?> annotation;
32+
33+
Type(Class<?> annotation) {
34+
this.annotation = annotation;
35+
}
36+
}
37+
38+
public SyntaxTree(SectionMeta owner) {
39+
this.owner = owner;
40+
this.handlers = new ArrayList<>();
41+
}
42+
43+
public List<Handler> getHandlers() {
44+
return handlers;
45+
}
46+
47+
public void addHandler(Handler handler) {
48+
this.handlers.add(handler);
49+
}
50+
51+
@Override
52+
public SectionMeta owner() {
53+
return owner;
54+
}
55+
56+
@Override
57+
public MultiLabel getEnd() {
58+
throw new IllegalStateException("This is not a programmatic tree.");
59+
}
60+
61+
@Override
62+
public void start(Context context) {
63+
64+
}
65+
66+
@Override
67+
public void branch(Context context) {
68+
69+
}
70+
71+
@Override
72+
public void close(Context context) {
73+
}
74+
75+
@Override
76+
public boolean permit(SyntaxElement element) {
77+
return element instanceof ICreateSyntax;
78+
}
79+
80+
@Override
81+
public boolean isOpen() {
82+
return false;
83+
}
84+
}

src/main/java/org/byteskript/skript/lang/syntax/entry/ReturnType.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import org.byteskript.skript.error.ScriptCompileError;
1414
import org.byteskript.skript.lang.element.StandardElements;
1515

16-
import java.util.regex.Matcher;
17-
1816
public class ReturnType extends SimpleEntry {
1917

2018
public ReturnType() {
@@ -43,10 +41,7 @@ public Pattern.Match match(String thing, Context context) {
4341
final String name = match.groups()[0].trim();
4442
if (name.contains("\""))
4543
throw new ScriptCompileError(context.lineNumber(), "Types should not be written inside quotation marks.");
46-
final String quote = java.util.regex.Pattern.quote(thing);
47-
final Matcher matcher = java.util.regex.Pattern.compile(quote).matcher(thing);
48-
matcher.find();
49-
return new Pattern.Match(matcher, name);
44+
return new Pattern.Match(Pattern.fakeMatcher(thing), name);
5045
}
5146

5247
@Override

src/main/java/org/byteskript/skript/lang/syntax/entry/Trigger.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import mx.kenzie.foundation.Type;
1111
import mx.kenzie.foundation.WriteInstruction;
1212
import mx.kenzie.foundation.compiler.State;
13-
import org.byteskript.skript.api.syntax.Section;
13+
import org.byteskript.skript.api.syntax.SectionEntry;
1414
import org.byteskript.skript.api.syntax.TriggerHolder;
1515
import org.byteskript.skript.compiler.*;
1616
import org.byteskript.skript.compiler.structure.PreVariable;
@@ -19,17 +19,12 @@
1919
import org.byteskript.skript.lang.element.StandardElements;
2020
import org.byteskript.skript.runtime.type.AtomicVariable;
2121

22-
public class Trigger extends Section {
22+
public class Trigger extends SectionEntry {
2323

2424
public Trigger() {
2525
super(SkriptLangSpec.LIBRARY, StandardElements.SECTION, "trigger");
2626
}
2727

28-
@Override
29-
public boolean allowAsInputFor(Type type) {
30-
return false;
31-
}
32-
3328
@Override
3429
public Pattern.Match match(String thing, Context context) {
3530
return super.match(thing, context);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2021 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.lang.syntax.entry.syntax;
8+
9+
import mx.kenzie.foundation.compiler.State;
10+
import org.byteskript.skript.api.syntax.SimpleEntry;
11+
import org.byteskript.skript.compiler.*;
12+
import org.byteskript.skript.compiler.structure.SyntaxTree;
13+
import org.byteskript.skript.error.ScriptCompileError;
14+
import org.byteskript.skript.lang.element.StandardElements;
15+
16+
public class EffectEntry extends SimpleEntry {
17+
18+
public EffectEntry() {
19+
super(SkriptLangSpec.LIBRARY, StandardElements.METADATA, "effect: %Pattern%");
20+
}
21+
22+
@Override
23+
public void compile(Context context, Pattern.Match match) {
24+
final String pattern = (String) match.meta();
25+
final SyntaxTree tree = ((SyntaxTree) context.getCurrentTree());
26+
tree.addHandler(new SyntaxTree.Handler(SyntaxTree.Type.EFFECT, pattern));
27+
context.setState(CompileState.MEMBER_BODY);
28+
}
29+
30+
@Override
31+
public Pattern.Match match(String thing, Context context) {
32+
if (!thing.startsWith("effect: ")) return null;
33+
final String raw = thing.substring(8).trim();
34+
if (raw.isEmpty()) throw new ScriptCompileError(context.lineNumber(), "No pattern was specified.");
35+
if (thing.contains("\""))
36+
throw new ScriptCompileError(context.lineNumber(), "Patterns should not contain quotation marks.");
37+
return new Pattern.Match(Pattern.fakeMatcher(thing), raw);
38+
}
39+
40+
@Override
41+
public boolean allowedIn(State state, Context context) {
42+
return super.allowedIn(state, context) && context.hasFlag(AreaFlag.IN_SYNTAX);
43+
}
44+
45+
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2021 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.lang.syntax.entry.syntax;
8+
9+
import mx.kenzie.foundation.compiler.State;
10+
import org.byteskript.skript.api.syntax.SimpleEntry;
11+
import org.byteskript.skript.compiler.*;
12+
import org.byteskript.skript.compiler.structure.SyntaxTree;
13+
import org.byteskript.skript.error.ScriptCompileError;
14+
import org.byteskript.skript.lang.element.StandardElements;
15+
16+
public class ExpressionEntry extends SimpleEntry {
17+
18+
public ExpressionEntry() {
19+
super(SkriptLangSpec.LIBRARY, StandardElements.METADATA, "expression: %Pattern%");
20+
}
21+
22+
@Override
23+
public void compile(Context context, Pattern.Match match) {
24+
final String pattern = (String) match.meta();
25+
final SyntaxTree tree = ((SyntaxTree) context.getCurrentTree());
26+
tree.addHandler(new SyntaxTree.Handler(SyntaxTree.Type.EXPRESSION, pattern));
27+
context.setState(CompileState.MEMBER_BODY);
28+
}
29+
30+
@Override
31+
public Pattern.Match match(String thing, Context context) {
32+
if (!thing.startsWith("expression: ")) return null;
33+
final String raw = thing.substring(12).trim();
34+
if (raw.isEmpty()) throw new ScriptCompileError(context.lineNumber(), "No pattern was specified.");
35+
if (thing.contains("\""))
36+
throw new ScriptCompileError(context.lineNumber(), "Patterns should not contain quotation marks.");
37+
return new Pattern.Match(Pattern.fakeMatcher(thing), raw);
38+
}
39+
40+
@Override
41+
public boolean allowedIn(State state, Context context) {
42+
return super.allowedIn(state, context) && context.hasFlag(AreaFlag.IN_SYNTAX);
43+
}
44+
45+
46+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2021 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.lang.syntax.entry.syntax;
8+
9+
/**
10+
* Used to spot syntax creators.
11+
*/
12+
public interface ICreateSyntax {
13+
}

0 commit comments

Comments
 (0)