Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: re-implement TextMate parser #623

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
*/
package org.eclipse.tm4e.core.internal.grammar.raw;

import java.util.HashMap;

import org.eclipse.tm4e.core.internal.parser.PropertySettable;

public final class RawCaptures extends HashMap<String, IRawRule> implements IRawCaptures, PropertySettable<IRawRule> {
public final class RawCaptures extends PropertySettable.HashMap<IRawRule> implements IRawCaptures {

private static final long serialVersionUID = 1L;

Expand All @@ -26,9 +24,4 @@ public IRawRule getCapture(final String captureId) {
public Iterable<String> getCaptureIds() {
return keySet();
}

@Override
public void setProperty(final String name, final IRawRule value) {
put(name, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
*/
package org.eclipse.tm4e.core.internal.grammar.raw;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
Expand All @@ -23,7 +21,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tm4e.core.internal.parser.PropertySettable;

public final class RawGrammar extends HashMap<String, @Nullable Object> implements IRawGrammar, PropertySettable<Object> {
public final class RawGrammar extends PropertySettable.HashMap<@Nullable Object> implements IRawGrammar {

private static final String FILE_TYPES = "fileTypes";
private static final String FIRST_LINE_MATCH = "firstLineMatch";
Expand Down Expand Up @@ -131,11 +129,6 @@ public void putAll(@Nullable final Map<? extends String, ? extends @Nullable Obj
super.putAll(m);
}

@Override
public void setProperty(final String name, final Object value) {
put(name, value);
}

@Override
public void setRepository(final IRawRepository repository) {
super.put(RawRule.REPOSITORY, repository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,46 @@
*/
package org.eclipse.tm4e.core.internal.grammar.raw;

import org.eclipse.tm4e.core.internal.parser.PListParser;
import org.eclipse.tm4e.core.internal.parser.PListParserJSON;
import org.eclipse.tm4e.core.internal.parser.PListParserXML;
import org.eclipse.tm4e.core.internal.parser.PListParserYAML;
import org.eclipse.tm4e.core.internal.parser.PListPath;
import java.util.List;

import org.eclipse.tm4e.core.internal.parser.PropertySettable;
import org.eclipse.tm4e.core.internal.parser.TMParser;
import org.eclipse.tm4e.core.internal.parser.TMParser.ObjectFactory;
import org.eclipse.tm4e.core.internal.parser.TMParserJSON;
import org.eclipse.tm4e.core.internal.parser.TMParserPList;
import org.eclipse.tm4e.core.internal.parser.TMParserYAML;
import org.eclipse.tm4e.core.registry.IGrammarSource;

/**
* TextMate Grammar reader utilities.
*/
public final class RawGrammarReader {

public static final PropertySettable.Factory<PListPath> OBJECT_FACTORY = path -> {
if (path.size() == 0) {
public static final ObjectFactory<RawGrammar> OBJECT_FACTORY = new ObjectFactory<>() {
@Override
public RawGrammar createRoot() {
return new RawGrammar();
}
return switch (path.last()) {
case RawRule.REPOSITORY -> new RawRepository();
case RawRule.BEGIN_CAPTURES, RawRule.CAPTURES, RawRule.END_CAPTURES, RawRule.WHILE_CAPTURES -> new RawCaptures();
default -> new RawRule();
};
};

private static final PListParser<RawGrammar> JSON_PARSER = new PListParserJSON<>(OBJECT_FACTORY);
private static final PListParser<RawGrammar> XML_PARSER = new PListParserXML<>(OBJECT_FACTORY);
private static final PListParser<RawGrammar> YAML_PARSER = new PListParserYAML<>(OBJECT_FACTORY);
@Override
public PropertySettable<?> createChild(final TMParser.PropertyPath path, final Class<?> sourceType) {
return switch (path.last().toString()) {
case RawRule.REPOSITORY -> new RawRepository();
case RawRule.BEGIN_CAPTURES, RawRule.CAPTURES, RawRule.END_CAPTURES, RawRule.WHILE_CAPTURES -> new RawCaptures();
default -> List.class.isAssignableFrom(sourceType)
? new PropertySettable.ArrayList<>()
: new RawRule();
};
}
};

public static IRawGrammar readGrammar(final IGrammarSource source) throws Exception {
try (var reader = source.getReader()) {
switch (source.getContentType()) {
case JSON:
return JSON_PARSER.parse(reader);
case YAML:
return YAML_PARSER.parse(reader);
case XML:
default:
return XML_PARSER.parse(reader);
}
return switch (source.getContentType()) {
case JSON -> TMParserJSON.INSTANCE.parse(reader, OBJECT_FACTORY);
case YAML -> TMParserYAML.INSTANCE.parse(reader, OBJECT_FACTORY);
default -> TMParserPList.INSTANCE.parse(reader, OBJECT_FACTORY);
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
*/
package org.eclipse.tm4e.core.internal.grammar.raw;

import java.util.HashMap;
import java.util.NoSuchElementException;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tm4e.core.internal.parser.PropertySettable;

public final class RawRepository extends HashMap<String, IRawRule> implements IRawRepository, PropertySettable<IRawRule> {
public final class RawRepository extends PropertySettable.HashMap<IRawRule> implements IRawRepository {

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -65,9 +64,4 @@ public void putEntries(final PropertySettable<IRawRule> target) {
target.setProperty(entry.getKey(), entry.getValue());
}
}

@Override
public void setProperty(final String name, final IRawRule value) {
put(name, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
package org.eclipse.tm4e.core.internal.grammar.raw;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tm4e.core.internal.parser.PropertySettable;
import org.eclipse.tm4e.core.internal.rule.RuleId;

public class RawRule extends HashMap<String, @Nullable Object> implements IRawRule, PropertySettable<Object> {
public class RawRule extends PropertySettable.HashMap<@Nullable Object> implements IRawRule {

private static final String APPLY_END_PATTERN_LAST = "applyEndPatternLast";
private static final String BEGIN = "begin";
Expand Down Expand Up @@ -175,9 +174,4 @@ public boolean isApplyEndPatternLast() {
}
return false;
}

@Override
public void setProperty(final String name, final Object value) {
put(name, value);
}
}
Loading
Loading