-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generator fragment for textmate grammars
Signed-off-by: Sebastian Zarnekow <sebastian.zarnekow@gmail.com>
- Loading branch information
Showing
11 changed files
with
842 additions
and
1 deletion.
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
87 changes: 87 additions & 0 deletions
87
...clipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/textmate/AutoRule.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,87 @@ | ||
/** | ||
* Copyright (c) 2024 Sigasi (http://www.sigasi.com) and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.xtext.xtext.generator.textmate; | ||
|
||
import java.util.Optional; | ||
|
||
import org.eclipse.emf.mwe2.runtime.Mandatory; | ||
import org.eclipse.xtext.AbstractElement; | ||
import org.eclipse.xtext.Grammar; | ||
import org.eclipse.xtext.GrammarUtil; | ||
import org.eclipse.xtext.Group; | ||
import org.eclipse.xtext.TerminalRule; | ||
import org.eclipse.xtext.UntilToken; | ||
|
||
/** | ||
* A TextMate rule that will parse the associated terminal rule and infer the TextMate equivalent automatically, if possible. | ||
* | ||
* @author David Medina | ||
* @author Sebastian Zarnekow | ||
* @since 2.35 | ||
*/ | ||
public class AutoRule extends TextMateRule { | ||
|
||
@Mandatory | ||
@Override | ||
public void setTerminalRule(String terminalRule) { | ||
super.setTerminalRule(terminalRule); | ||
} | ||
|
||
public Optional<TextMateRule> init(Grammar grammar, boolean ignoreCase, TerminalRuleToTextMateRule generator) { | ||
TerminalRule terminal = (TerminalRule) GrammarUtil.findRuleForName(grammar, getTerminalRule()); | ||
if (terminal != null) { | ||
Optional<TextMateRule> result = toBeginEndRule(terminal, generator).or(()->toMatchRule(terminal, generator)); | ||
return result.map(r->{ | ||
r.setName(Optional.ofNullable(getName()).orElseGet(()->toTextMateName(terminal, GrammarUtil.getSimpleName(grammar).toLowerCase()))); | ||
return r; | ||
}).filter(r->r.getName() != null); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
protected Optional<TextMateRule> toBeginEndRule(TerminalRule rule, TerminalRuleToTextMateRule generator) { | ||
AbstractElement alternatives = rule.getAlternatives(); | ||
if (alternatives instanceof Group) { | ||
Group group = (Group) alternatives; | ||
if (group.getElements().size() == 2 && group.getElements().get(1) instanceof UntilToken) { | ||
String begin = generator.getMatchRegEx(group.getElements().get(0)); | ||
String end = generator.getMatchRegEx(((UntilToken)group.getElements().get(1)).getTerminal()); | ||
BeginEndRule result = new BeginEndRule(); | ||
result.setBegin(begin); | ||
result.setEnd(end); | ||
return Optional.of(result); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
protected Optional<TextMateRule> toMatchRule(TerminalRule rule, TerminalRuleToTextMateRule generator) { | ||
try { | ||
String match = generator.getMatchRegEx(rule); | ||
MatchRule result = new MatchRule(); | ||
result.setMatch(match); | ||
return Optional.of(result); | ||
} catch(Exception e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
protected String toTextMateName(TerminalRule terminal, String langName) { | ||
switch(terminal.getName()) { | ||
case "SL_COMMENT": return "comment.line." + langName; | ||
case "ML_COMMENT": return "comment.block." + langName; | ||
case "STRING": return "string.quoted." + langName; | ||
case "ID": return "variable." + langName; | ||
case "INT": return "constant.numeric." + langName; | ||
case "ANY_OTHER": return "invalid." + langName; | ||
} | ||
return null; | ||
} | ||
|
||
} |
109 changes: 109 additions & 0 deletions
109
...se.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/textmate/BeginEndRule.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,109 @@ | ||
/** | ||
* Copyright (c) 2024 Sigasi (http://www.sigasi.com) and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.xtext.xtext.generator.textmate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
import org.eclipse.emf.mwe2.runtime.Mandatory; | ||
|
||
import com.google.gson.annotations.Expose; | ||
|
||
/** | ||
* See the <a href="https://macromates.com/manual/en/language_grammars#rule_keys">TextMate specification</a>. | ||
* | ||
* @author David Medina | ||
* @author Sebastian Zarnekow | ||
* @since 2.35 | ||
*/ | ||
public class BeginEndRule extends TextMateRule { | ||
|
||
@Expose private List<TextMateRule> patterns; | ||
@Expose private String begin; | ||
@Expose private String end; | ||
@Expose private String contentName; | ||
@Expose private Map<Integer, Capture> captures; | ||
@Expose private Map<Integer, Capture> beginCaptures; | ||
@Expose private Map<Integer, Capture> endCaptures; | ||
|
||
@Mandatory | ||
@Override | ||
public void setName(String name) { | ||
super.setName(name); | ||
} | ||
|
||
public String getBegin() { | ||
return begin; | ||
} | ||
@Mandatory | ||
public void setBegin(String begin) { | ||
this.begin = begin; | ||
} | ||
public String getEnd() { | ||
return end; | ||
} | ||
@Mandatory | ||
public void setEnd(String end) { | ||
this.end = end; | ||
} | ||
|
||
public void addMatchPattern(MatchRule rule) { | ||
if (patterns == null) { | ||
patterns = new ArrayList<>(); | ||
} | ||
this.patterns.add(rule); | ||
} | ||
|
||
public void addBeginEndPattern(BeginEndRule rule) { | ||
if (patterns == null) { | ||
patterns = new ArrayList<>(); | ||
} | ||
this.patterns.add(rule); | ||
} | ||
|
||
public String getContentName() { | ||
return contentName; | ||
} | ||
|
||
public void setContentName(String contentName) { | ||
this.contentName = contentName; | ||
} | ||
|
||
public void addInclude(String include) { | ||
if (patterns == null) { | ||
patterns = new ArrayList<>(); | ||
} | ||
this.patterns.add(new IncludeRule(include)); | ||
} | ||
|
||
public void addBeginEndCapture(Capture c) { | ||
if (beginCaptures == null) { | ||
beginCaptures = new TreeMap<>(); | ||
} | ||
beginCaptures.put(c.getGroup(), c); | ||
} | ||
|
||
public void addEndCapture(Capture c) { | ||
if (endCaptures == null) { | ||
endCaptures = new TreeMap<>(); | ||
} | ||
endCaptures.put(c.getGroup(), c); | ||
} | ||
|
||
public void addCapture(Capture c) { | ||
if (captures == null) { | ||
captures = new TreeMap<>(); | ||
} | ||
captures.put(c.getGroup(), c); | ||
} | ||
|
||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/textmate/Capture.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,38 @@ | ||
/** | ||
* Copyright (c) 2024 Sigasi (http://www.sigasi.com) and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.xtext.xtext.generator.textmate; | ||
|
||
import com.google.gson.annotations.Expose; | ||
|
||
/** | ||
* See the <a href="https://macromates.com/manual/en/language_grammars#rule_keys">TextMate specification</a>. | ||
* | ||
* @author David Medina | ||
* @author Sebastian Zarnekow | ||
* @since 2.35 | ||
*/ | ||
public class Capture { | ||
|
||
private int group; | ||
@Expose private String name; | ||
|
||
public int getGroup() { | ||
return group; | ||
} | ||
public void setGroup(int group) { | ||
this.group = group; | ||
} | ||
public String getName() { | ||
return name; | ||
} | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...pse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/textmate/IncludeRule.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,38 @@ | ||
/** | ||
* Copyright (c) 2024 Sigasi (http://www.sigasi.com) and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.xtext.xtext.generator.textmate; | ||
|
||
import com.google.gson.annotations.Expose; | ||
|
||
/** | ||
* See the <a href="https://macromates.com/manual/en/language_grammars#rule_keys">TextMate specification</a>. | ||
* | ||
* @author David Medina | ||
* @author Sebastian Zarnekow | ||
* @since 2.35 | ||
*/ | ||
public class IncludeRule extends TextMateRule { | ||
|
||
@Expose private String include; | ||
|
||
public IncludeRule(String include) { | ||
this.include = include; | ||
} | ||
|
||
public String getInclude() { | ||
return include; | ||
} | ||
|
||
public void setInclude(String include) { | ||
this.include = include; | ||
} | ||
|
||
|
||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...lipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/textmate/MatchRule.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,53 @@ | ||
/** | ||
* Copyright (c) 2024 Sigasi (http://www.sigasi.com) and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.xtext.xtext.generator.textmate; | ||
|
||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
import org.eclipse.emf.mwe2.runtime.Mandatory; | ||
|
||
import com.google.gson.annotations.Expose; | ||
|
||
/** | ||
* See the <a href="https://macromates.com/manual/en/language_grammars#rule_keys">TextMate specification</a>. | ||
* | ||
* @author David Medina | ||
* @author Sebastian Zarnekow | ||
* @since 2.35 | ||
*/ | ||
public class MatchRule extends TextMateRule { | ||
|
||
@Expose private String match; | ||
@Expose private Map<Integer, Capture> captures; | ||
|
||
@Mandatory | ||
@Override | ||
public void setName(String name) { | ||
super.setName(name); | ||
} | ||
|
||
public String getMatch() { | ||
return match; | ||
} | ||
|
||
@Mandatory | ||
public void setMatch(String match) { | ||
this.match = match; | ||
} | ||
|
||
public void addCapture(Capture c) { | ||
if (captures == null) { | ||
captures = new TreeMap<>(); | ||
} | ||
captures.put(c.getGroup(), c); | ||
} | ||
|
||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...pse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/textmate/SkippedRule.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 @@ | ||
/** | ||
* Copyright (c) 2024 Sigasi (http://www.sigasi.com) and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.xtext.xtext.generator.textmate; | ||
|
||
/** | ||
* Explicitely skip a terminal rule from auto-processing. | ||
* | ||
* @author David Medina | ||
* @author Sebastian Zarnekow | ||
* @since 2.35 | ||
*/ | ||
public class SkippedRule extends TextMateRule { | ||
|
||
} |
Oops, something went wrong.