-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dd235f
commit f95996f
Showing
8 changed files
with
261 additions
and
16 deletions.
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
src/main/java/org/cyclops/integratedscripting/api/language/ILanguageHandler.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,20 @@ | ||
package org.cyclops.integratedscripting.api.language; | ||
|
||
import net.minecraft.network.chat.Style; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Logic for handling a specific programming language. | ||
* @author rubensworks | ||
*/ | ||
public interface ILanguageHandler { | ||
|
||
public String getName(); | ||
|
||
public List<String> getExtensions(); | ||
|
||
public List<Pair<Style, String>> markupLine(String line); | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/cyclops/integratedscripting/api/language/ILanguageHandlerRegistry.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,19 @@ | ||
package org.cyclops.integratedscripting.api.language; | ||
|
||
import org.cyclops.cyclopscore.init.IRegistry; | ||
|
||
import javax.annotation.Nullable; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Registry for {@link ILanguageHandler}'s. | ||
* @author rubensworks | ||
*/ | ||
public interface ILanguageHandlerRegistry extends IRegistry { | ||
|
||
public void register(ILanguageHandler provider); | ||
|
||
@Nullable | ||
public ILanguageHandler getProvider(Path filePath); | ||
|
||
} |
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
92 changes: 92 additions & 0 deletions
92
src/main/java/org/cyclops/integratedscripting/core/language/LanguageHandlerJavaScript.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,92 @@ | ||
package org.cyclops.integratedscripting.core.language; | ||
|
||
import com.google.common.collect.Maps; | ||
import net.minecraft.network.chat.Style; | ||
import net.minecraft.network.chat.TextColor; | ||
import org.apache.commons.compress.utils.Lists; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.cyclops.cyclopscore.helper.Helpers; | ||
import org.cyclops.integratedscripting.api.language.ILanguageHandler; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author rubensworks | ||
*/ | ||
public class LanguageHandlerJavaScript implements ILanguageHandler { | ||
|
||
// These colors are inspired by GitHub's color palette | ||
public static final Style ATTRIBUTE = Style.EMPTY.withColor(TextColor.fromRgb(Helpers.RGBToInt(121, 93, 163))); // Purple | ||
public static final Style COMMENT = Style.EMPTY.withColor(TextColor.fromRgb(Helpers.RGBToInt(150, 152, 150))); // Gray | ||
public static final Style SYMBOL = Style.EMPTY.withColor(TextColor.fromRgb(Helpers.RGBToInt(99, 163, 92))); // Green | ||
public static final Style CONSTANT = Style.EMPTY.withColor(TextColor.fromRgb(Helpers.RGBToInt(0, 134, 179))); // Blue | ||
public static final Style KEYWORD = Style.EMPTY.withColor(TextColor.fromRgb(Helpers.RGBToInt(167, 29, 93))); // Red-purple | ||
|
||
public final Map<String, Style> tokenStyles; | ||
|
||
public LanguageHandlerJavaScript() { | ||
this.tokenStyles = Maps.newHashMap(); | ||
|
||
this.tokenStyles.put("const", KEYWORD); | ||
this.tokenStyles.put("let", KEYWORD); | ||
this.tokenStyles.put("var", KEYWORD); | ||
this.tokenStyles.put("function", KEYWORD); | ||
this.tokenStyles.put("return", KEYWORD); | ||
this.tokenStyles.put("true", KEYWORD); | ||
this.tokenStyles.put("false", KEYWORD); | ||
|
||
this.tokenStyles.put("{", SYMBOL); | ||
this.tokenStyles.put("}", SYMBOL); | ||
this.tokenStyles.put("(", SYMBOL); | ||
this.tokenStyles.put(")", SYMBOL); | ||
this.tokenStyles.put("[", SYMBOL); | ||
this.tokenStyles.put("]", SYMBOL); | ||
this.tokenStyles.put(".", SYMBOL); | ||
this.tokenStyles.put(";", SYMBOL); | ||
this.tokenStyles.put("=", SYMBOL); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "JavaScript"; | ||
} | ||
|
||
@Override | ||
public List<String> getExtensions() { | ||
return Arrays.stream(new String[]{ "js", "cjs", "mjs" }).toList(); | ||
} | ||
|
||
@Override | ||
public List<Pair<Style, String>> markupLine(String line) { | ||
// TODO: in the future, write/use JS lexer/tokenizer | ||
|
||
List<Pair<Style, String>> segments = Lists.newArrayList(); | ||
segments.add(Pair.of(Style.EMPTY, line)); | ||
|
||
// Split up string by tokens | ||
for (Map.Entry<String, Style> entry : this.tokenStyles.entrySet()) { | ||
String token = entry.getKey(); | ||
Style tokenStyle = entry.getValue(); | ||
List<Pair<Style, String>> segmentsNew = Lists.newArrayList(); | ||
|
||
for (Pair<Style, String> segment : segments) { | ||
Style styleOriginal = segment.getLeft(); | ||
String hayStack = segment.getRight(); | ||
int pos; | ||
while ((pos = hayStack.indexOf(token)) >= 0) { | ||
segmentsNew.add(Pair.of(styleOriginal, hayStack.substring(0, pos))); | ||
segmentsNew.add(Pair.of(tokenStyle, hayStack.substring(pos, pos + token.length()))); | ||
hayStack = hayStack.substring(pos + token.length()); | ||
} | ||
|
||
segmentsNew.add(Pair.of(styleOriginal, hayStack)); | ||
} | ||
|
||
segments = segmentsNew; | ||
} | ||
|
||
return segments; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/org/cyclops/integratedscripting/core/language/LanguageHandlerRegistry.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,48 @@ | ||
package org.cyclops.integratedscripting.core.language; | ||
|
||
import com.google.common.collect.Maps; | ||
import org.cyclops.integratedscripting.api.language.ILanguageHandler; | ||
import org.cyclops.integratedscripting.api.language.ILanguageHandlerRegistry; | ||
|
||
import javax.annotation.Nullable; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author rubensworks | ||
*/ | ||
public class LanguageHandlerRegistry implements ILanguageHandlerRegistry { | ||
|
||
private static LanguageHandlerRegistry INSTANCE = new LanguageHandlerRegistry(); | ||
|
||
private final Map<String, ILanguageHandler> extensionToHandlerMap = Maps.newHashMap(); | ||
|
||
private LanguageHandlerRegistry() { | ||
} | ||
|
||
/** | ||
* @return The unique instance. | ||
*/ | ||
public static LanguageHandlerRegistry getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public void register(ILanguageHandler translator) { | ||
for (String extension : translator.getExtensions()) { | ||
extensionToHandlerMap.put(extension, translator); | ||
} | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public ILanguageHandler getProvider(Path filePath) { | ||
String filePathString = filePath.toString(); | ||
int dotPos = filePathString.lastIndexOf('.'); | ||
if (dotPos >= 0 && dotPos + 1 < filePathString.length()) { | ||
String extension = filePathString.substring(dotPos + 1); | ||
return extensionToHandlerMap.get(extension); | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.