Skip to content

Commit

Permalink
Initialize Markdown project with marked parser ported in Java. See
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Dec 23, 2016
1 parent 698a4e0 commit 8b5eecb
Show file tree
Hide file tree
Showing 37 changed files with 2,637 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 193,7 @@ public ITokenizeLineResult tokenizeLine(String lineText, StackElement prevState)
OnigString onigLineText = GrammarHelper.createOnigString(lineText);
int lineLength = lineText.length();
LineTokens lineTokens = new LineTokens();
StackElement nextState = Tokenizer._tokenizeString(this, onigLineText, isFirstLine, 0, prevState, lineTokens);
StackElement nextState = LineTokenizer._tokenizeString(this, onigLineText, isFirstLine, 0, prevState, lineTokens);

IToken[] _produced = lineTokens.getResult(nextState, lineLength);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 31,7 @@
import org.eclipse.tm4e.core.internal.rule.MatchRule;
import org.eclipse.tm4e.core.internal.rule.Rule;

class Tokenizer {
class LineTokenizer {

class WhileStack {

Expand Down Expand Up @@ -69,7 69,7 @@ public WhileCheckResult(StackElement stack, int linePos, int anchorPosition, boo
private boolean STOP;
private final int lineLength;

public Tokenizer(Grammar grammar, OnigString lineText, boolean isFirstLine, int linePos, StackElement stack,
public LineTokenizer(Grammar grammar, OnigString lineText, boolean isFirstLine, int linePos, StackElement stack,
LineTokens lineTokens) {
this.grammar = grammar;
this.lineText = lineText;
Expand Down Expand Up @@ -492,6 492,6 @@ private WhileCheckResult _checkWhileConditions(Grammar grammar, OnigString lineT

public static StackElement _tokenizeString(Grammar grammar, OnigString lineText, boolean isFirstLine, int linePos,
StackElement stack, LineTokens lineTokens) {
return new Tokenizer(grammar, lineText, isFirstLine, linePos, stack, lineTokens).scan();
return new LineTokenizer(grammar, lineText, isFirstLine, linePos, stack, lineTokens).scan();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 54,8 @@ public abstract class AbstractTMModel implements ITMModel {

private boolean _isDisposing;

private Tokenizer tokenizer;

public AbstractTMModel() {
this._decodeMap = new DecodeMap();
this.listeners = new ArrayList<>();
Expand Down Expand Up @@ -304,6 306,7 @@ public IGrammar getGrammar() {
@Override
public void setGrammar(IGrammar grammar) {
this.grammar = grammar;
this.tokenizer = new Tokenizer(grammar);
}

@Override
Expand Down Expand Up @@ -420,7 423,7 @@ private void _resetTokenizationState() {
for (ModelLine line : this.lines) {
line.resetTokenizationState();
}
lines.get(0).setState(new TMState(null, null));
lines.get(0).setState(tokenizer.getInitialState());
this._invalidLineStartIndex = 0;
this._beginBackgroundTokenization();
}
Expand Down Expand Up @@ -526,7 529,7 @@ private void _updateTokensUntilLine(ModelTokensChangedEventBuilder eventBuilder,
try {
text = getLineText(lineIndex);
// Tokenize only the first X characters
r = tokenize(text, modeLine.getState(), 0, stopLineTokenizationAfter);
r = tokenizer.tokenize(text, modeLine.getState(), 0, stopLineTokenizationAfter);
} catch (Throwable e) {
// e.friendlyMessage =
// TextModelWithTokens.MODE_TOKENIZATION_FAILED_MSG;
Expand Down Expand Up @@ -609,76 612,6 @@ private LineTokens nullTokenize(String buffer, TMState state) {

// TMSyntax

private LineTokens tokenize(String line, TMState state, int offsetDelta, int stopLineTokenizationAfter) {
// Do not attempt to tokenize if a line has over 20k
// or if the rule stack contains more than 100 rules (indicator of
// broken grammar that forgets to pop rules)
// if (line.length >= 20000 || depth(state.getRuleStack()) > 100) {
// return new LineTokens(
// [new Token(offsetDelta, '')],
// [new ModeTransition(offsetDelta, state.getModeId())],
// offsetDelta,
// state
// );
// }

if (grammar == null) {
throw new TMException("No TextMate grammar defined");
}
TMState freshState = state.clone();
ITokenizeLineResult textMateResult = grammar.tokenizeLine(line, freshState.getRuleStack());
freshState.setRuleStack(textMateResult.getRuleStack());

// Create the result early and fill in the tokens later
List<TMToken> tokens = new ArrayList<>();
String lastTokenType = null;
for (int tokenIndex = 0, len = textMateResult.getTokens().length; tokenIndex < len; tokenIndex ) {
IToken token = textMateResult.getTokens()[tokenIndex];
int tokenStartIndex = token.getStartIndex();
String tokenType = decodeTextMateToken(this._decodeMap, token.getScopes().toArray(new String[0]));

// do not push a new token if the type is exactly the same (also
// helps with ligatures)
if (!tokenType.equals(lastTokenType)) {
tokens.add(new TMToken(tokenStartIndex offsetDelta, tokenType));
lastTokenType = tokenType;
}
}
return new LineTokens(tokens, offsetDelta line.length(), freshState);
}

private String decodeTextMateToken(DecodeMap decodeMap, String[] scopes) {
String[] prevTokenScopes = decodeMap.prevToken.scopes;
int prevTokenScopesLength = prevTokenScopes.length;
Map<Integer, Map<Integer, Boolean>> prevTokenScopeTokensMaps = decodeMap.prevToken.scopeTokensMaps;

Map<Integer, Map<Integer, Boolean>> scopeTokensMaps = new LinkedHashMap<>();
Map<Integer, Boolean> prevScopeTokensMaps = new LinkedHashMap<>();
boolean sameAsPrev = true;
for (int level = 1/* deliberately skip scope 0 */; level < scopes.length; level ) {
String scope = scopes[level];

if (sameAsPrev) {
if (level < prevTokenScopesLength && prevTokenScopes[level].equals(scope)) {
prevScopeTokensMaps = prevTokenScopeTokensMaps.get(level);
scopeTokensMaps.put(level, prevScopeTokensMaps);
continue;
}
sameAsPrev = false;
}

int[] tokens = decodeMap.getTokenIds(scope);
prevScopeTokensMaps = new LinkedHashMap<>(prevScopeTokensMaps);
for (int i = 0; i < tokens.length; i ) {
prevScopeTokensMaps.put(tokens[i], true);
}
scopeTokensMaps.put(level, prevScopeTokensMaps);
}

decodeMap.prevToken = new TMTokenDecodeData(scopes, scopeTokensMaps);
return decodeMap.getToken(prevScopeTokensMaps);
}

public List<TMToken> getLineTokens(int lineNumber) {
_withModelTokensChangedEventBuilder((eventBuilder) -> {
_updateTokensUntilLine(eventBuilder, lineNumber, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 1,14 @@
package org.eclipse.tm4e.core.model;

public interface ITokenizationSupport {

TMState getInitialState();

LineTokens tokenize(String line, TMState state);

// add offsetDelta to each of the returned indices
// stop tokenizing at absolute value stopAtOffset (i.e. stream.pos()
// offsetDelta > stopAtOffset)
LineTokens tokenize(String line, TMState state, Integer offsetDelta, Integer stopAtOffset);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 22,7 @@ public void setEndState(TMState endState) {
this.endState = endState;
}

public List<TMToken> getTokens() {
return tokens;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 1,102 @@
package org.eclipse.tm4e.core.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.tm4e.core.grammar.IGrammar;
import org.eclipse.tm4e.core.grammar.IToken;
import org.eclipse.tm4e.core.grammar.ITokenizeLineResult;

public class Tokenizer implements ITokenizationSupport {

private final IGrammar grammar;
private final DecodeMap decodeMap;

public Tokenizer(IGrammar grammar) {
this.grammar = grammar;
this.decodeMap = new DecodeMap();
}

@Override
public TMState getInitialState() {
return new TMState(null, null);
}

@Override
public LineTokens tokenize(String line, TMState state) {
return tokenize(line, state, null, null);
}

@Override
public LineTokens tokenize(String line, TMState state, Integer offsetDelta, Integer stopAtOffset) {
if (offsetDelta == null) {
offsetDelta = 0;
}
// Do not attempt to tokenize if a line has over 20k
// or if the rule stack contains more than 100 rules (indicator of
// broken grammar that forgets to pop rules)
// if (line.length >= 20000 || depth(state.ruleStack) > 100) {
// return new RawLineTokens(
// [new Token(offsetDelta, '')],
// [new ModeTransition(offsetDelta, this._modeId)],
// offsetDelta,
// state
// );
// }
TMState freshState = state.clone();
ITokenizeLineResult textMateResult = grammar.tokenizeLine(line, freshState.getRuleStack());
freshState.setRuleStack(textMateResult.getRuleStack());

// Create the result early and fill in the tokens later
List<TMToken> tokens = new ArrayList<>();
String lastTokenType = null;
for (int tokenIndex = 0, len = textMateResult.getTokens().length; tokenIndex < len; tokenIndex ) {
IToken token = textMateResult.getTokens()[tokenIndex];
int tokenStartIndex = token.getStartIndex();
String tokenType = decodeTextMateToken(this.decodeMap, token.getScopes().toArray(new String[0]));

// do not push a new token if the type is exactly the same (also
// helps with ligatures)
if (!tokenType.equals(lastTokenType)) {
tokens.add(new TMToken(tokenStartIndex offsetDelta, tokenType));
lastTokenType = tokenType;
}
}
return new LineTokens(tokens, offsetDelta line.length(), freshState);

}

private String decodeTextMateToken(DecodeMap decodeMap, String[] scopes) {
String[] prevTokenScopes = decodeMap.prevToken.scopes;
int prevTokenScopesLength = prevTokenScopes.length;
Map<Integer, Map<Integer, Boolean>> prevTokenScopeTokensMaps = decodeMap.prevToken.scopeTokensMaps;

Map<Integer, Map<Integer, Boolean>> scopeTokensMaps = new LinkedHashMap<>();
Map<Integer, Boolean> prevScopeTokensMaps = new LinkedHashMap<>();
boolean sameAsPrev = true;
for (int level = 1/* deliberately skip scope 0 */; level < scopes.length; level ) {
String scope = scopes[level];

if (sameAsPrev) {
if (level < prevTokenScopesLength && prevTokenScopes[level].equals(scope)) {
prevScopeTokensMaps = prevTokenScopeTokensMaps.get(level);
scopeTokensMaps.put(level, prevScopeTokensMaps);
continue;
}
sameAsPrev = false;
}

int[] tokens = decodeMap.getTokenIds(scope);
prevScopeTokensMaps = new LinkedHashMap<>(prevScopeTokensMaps);
for (int i = 0; i < tokens.length; i ) {
prevScopeTokensMaps.put(tokens[i], true);
}
scopeTokensMaps.put(level, prevScopeTokensMaps);
}

decodeMap.prevToken = new TMTokenDecodeData(scopes, scopeTokensMaps);
return decodeMap.getToken(prevScopeTokensMaps);
}
}
12 changes: 12 additions & 0 deletions org.eclipse.tm4e.markdown/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
2 changes: 2 additions & 0 deletions org.eclipse.tm4e.markdown/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 1,2 @@
/bin
/target
34 changes: 34 additions & 0 deletions org.eclipse.tm4e.markdown/.project
Original file line number Diff line number Diff line change
@@ -0,0 1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.tm4e.markdown</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
</natures>
</projectDescription>
14 changes: 14 additions & 0 deletions org.eclipse.tm4e.markdown/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 1,14 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-SymbolicName: org.eclipse.tm4e.markdown;singleton:=true
Bundle-Version: 1.0.0.qualifier
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.tm4e.core,
org.eclipse.tm4e.ui
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: org.eclipse.tm4e.markdown,
org.eclipse.tm4e.markdown.marked
Bundle-ActivationPolicy: lazy
5 changes: 5 additions & 0 deletions org.eclipse.tm4e.markdown/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
source.. = src/main/java/,\
src/main/resources/
bin.includes = META-INF/,\
.,\
plugin.properties
48 changes: 48 additions & 0 deletions org.eclipse.tm4e.markdown/marked.js/marked - Copie.html
Original file line number Diff line number Diff line change
@@ -0,0 1,48 @@
<html>

<head>

<script src="marked.js" > </script>

<script type="text/javascript">

function f() {

var html = marked(document.getElementById('a').value)
document.getElementById('result').value = html;

}
</script>

</head>
<body onload="f()">
<textarea id="a" onkeyup="f()" cols="100" rows="20" >Every `cf push` deploys applications to one particular Cloud Foundry instance. Every Cloud Foundry instance may have a shared domain set by an admin. Unless you specify a domain, Cloud Foundry incorporates that shared domain in the route to your application.

You can use the `domain` attribute when you want your application to be served from a domain other than the default shared domain.

```
---
...
domain: unique-example.com
```

The command line option that overrides this attribute is `-d`.

### The domains attribute

Use the `domains` attribute to provide multiple domains. If you define both `domain` and `domains` attributes, Cloud Foundry creates routes for domains defined in both of these fields.

```
---
...
domains:
- domain-example1.com
- domain-example2.org
```

The command line option that overrides this attribute is `-d`.</textarea>
<textarea id="result" cols="100" rows="20" >
</textarea>
</body>

</html>
Loading

0 comments on commit 8b5eecb

Please sign in to comment.