-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ EntryLoader (to customize the parsing process)
- Loading branch information
Mwexim
authored and
Mwexim
committed
Dec 21, 2021
1 parent
766be8a
commit ad33642
Showing
6 changed files
with
186 additions
and
61 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/io/github/syst3ms/skriptparser/lang/entries/EntryLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.syst3ms.skriptparser.lang.entries; | ||
|
||
import io.github.syst3ms.skriptparser.file.FileElement; | ||
import io.github.syst3ms.skriptparser.log.SkriptLogger; | ||
import io.github.syst3ms.skriptparser.parsing.ParserState; | ||
|
||
public abstract class EntryLoader { | ||
protected final String key; | ||
private final boolean optional; | ||
|
||
public EntryLoader(String key, boolean optional) { | ||
this.key = key; | ||
this.optional = optional; | ||
} | ||
|
||
/** | ||
* This {@link EntryLoader} will attempt to load the entry | ||
* using its {@linkplain FileElement}. One can use this method | ||
* to create specific error messages or to load the value correctly. | ||
* @param config the configuration | ||
* @param element the element | ||
* @param parserState the parser state | ||
* @param logger the logger | ||
* @return {@code true} if loaded successfully, {@code false} if an error occurred | ||
*/ | ||
public abstract boolean loadEntry(SectionConfiguration config, FileElement element, ParserState parserState, SkriptLogger logger); | ||
|
||
public boolean isOptional() { | ||
return optional; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/io/github/syst3ms/skriptparser/lang/entries/OptionLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.github.syst3ms.skriptparser.lang.entries; | ||
|
||
import io.github.syst3ms.skriptparser.file.FileElement; | ||
import io.github.syst3ms.skriptparser.file.FileSection; | ||
import io.github.syst3ms.skriptparser.file.VoidElement; | ||
import io.github.syst3ms.skriptparser.log.ErrorType; | ||
import io.github.syst3ms.skriptparser.log.SkriptLogger; | ||
import io.github.syst3ms.skriptparser.parsing.ParserState; | ||
|
||
public class OptionLoader extends EntryLoader { | ||
public static final String OPTION_SPLIT_PATTERN = ": "; | ||
|
||
private final boolean multiple; | ||
|
||
public OptionLoader(boolean multiple, String key, boolean optional) { | ||
super(key, optional); | ||
this.multiple = multiple; | ||
} | ||
|
||
@Override | ||
public boolean loadEntry(SectionConfiguration config, FileElement element, ParserState parserState, SkriptLogger logger) { | ||
var content = element.getLineContent().split(OPTION_SPLIT_PATTERN); | ||
if (content.length == 0) | ||
return false; | ||
var key = content[0]; | ||
var entry = content.length > 1 ? content[1] : null; | ||
|
||
if (!key.equalsIgnoreCase(this.key)) | ||
return false; | ||
if (element instanceof FileSection) { | ||
if (!multiple) { | ||
logger.error("The entry '" + key + "' does not support multiple values.", ErrorType.SEMANTIC_ERROR); | ||
return false; | ||
} else if (entry != null) { | ||
logger.error("The entry '" + key + "' has been configured incorrectly.", ErrorType.SEMANTIC_ERROR); | ||
return false; | ||
} | ||
config.getData().put(this.key, ((FileSection) element).getElements().stream() | ||
.filter(el -> !(el instanceof VoidElement)) | ||
.map(FileElement::getLineContent) | ||
.toArray(String[]::new) | ||
); | ||
} else { | ||
if (entry == null) { | ||
logger.error("The entry '" + key + "' has been configured incorrectly.", ErrorType.SEMANTIC_ERROR); | ||
return false; | ||
} | ||
config.getData().put(this.key, multiple ? new String[] {entry} : entry); | ||
} | ||
return true; | ||
} | ||
|
||
public boolean isMultiple() { | ||
return multiple; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/io/github/syst3ms/skriptparser/lang/entries/SectionLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.syst3ms.skriptparser.lang.entries; | ||
|
||
import io.github.syst3ms.skriptparser.file.FileElement; | ||
import io.github.syst3ms.skriptparser.file.FileSection; | ||
import io.github.syst3ms.skriptparser.log.ErrorType; | ||
import io.github.syst3ms.skriptparser.log.SkriptLogger; | ||
import io.github.syst3ms.skriptparser.parsing.ParserState; | ||
|
||
public class SectionLoader extends EntryLoader { | ||
public SectionLoader(String key, boolean optional) { | ||
super(key, optional); | ||
} | ||
|
||
@Override | ||
public boolean loadEntry(SectionConfiguration config, FileElement element, ParserState parserState, SkriptLogger logger) { | ||
if (!element.getLineContent().equalsIgnoreCase(this.key)) | ||
return false; | ||
if (!(element instanceof FileSection)) { | ||
logger.error("The entry '" + key + "' has been configured incorrectly.", ErrorType.SEMANTIC_ERROR); | ||
return false; | ||
} | ||
var entry = new EntrySection((FileSection) element, parserState, logger, element.getLineContent()); | ||
if (config.getParent() != null) | ||
entry.setParent(config.getParent()); | ||
config.getData().put(key, entry); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters