New architecture for syntax coloring

This commit is contained in:
jkohnlein 2008-10-05 12:47:04 +00:00
parent 38c7352aff
commit 3d393de36b
3 changed files with 88 additions and 0 deletions

View file

@ -21,6 +21,7 @@ import org.eclipse.xtext.crossref.impl.DefaultRuntimeURIChecker;
import org.eclipse.xtext.crossref.impl.XtextBuiltinFragmentProvider;
import org.eclipse.xtext.crossref.impl.XtextBuiltinLinkProvider;
import org.eclipse.xtext.crossref.internal.Linker;
import org.eclipse.xtext.parser.antlr.AntlrTokenDefProvider;
import org.eclipse.xtext.service.AbstractServiceRegistrationFactory;
/**
@ -39,6 +40,7 @@ public class XtextBuiltinRuntimeConfig extends AbstractServiceRegistrationFactor
.with(ILinkProvider.class, XtextBuiltinLinkProvider.class)
.with(IURIChecker.class, DefaultRuntimeURIChecker.class)
.with(IFragmentProvider.class, XtextBuiltinFragmentProvider.class)
.with(AntlrTokenDefProvider.class)
.registrations();
}

View file

@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.parser.antlr;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.xtext.service.Inject;
/**
* @author Jan Köhnlein - Initial contribution and API
*/
public class AntlrTokenDefProvider {
private static final Logger log = Logger.getLogger(AntlrTokenDefProvider.class);
@Inject
protected IAntlrTokenFileProvider antlrTokenFileProvider;
protected Map<Integer, String> tokenDefMap;
public Map<Integer, String> getTokenDefMap() {
if (tokenDefMap == null) {
InputStream tokenFile = antlrTokenFileProvider.getAntlrTokenFile();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(tokenFile));
tokenDefMap = new HashMap<Integer, String>();
String line = br.readLine();
Pattern pattern = Pattern.compile("(.*)=(\\d+)");
while (line != null) {
Matcher m = pattern.matcher(line);
if (!m.matches()) {
throw new IllegalStateException("Couldn't match line : '" + line + "'");
}
int antlrTokenType = Integer.parseInt(m.group(2));
String antlrTokenDef = m.group(1);
tokenDefMap.put(antlrTokenType, antlrTokenDef);
line = br.readLine();
}
} catch (Exception e) {
log.error(e);
tokenDefMap = null;
throw new WrappedException(e);
} finally {
try {
tokenFile.close();
} catch (IOException e) {
throw new WrappedException(e);
}
}
}
return tokenDefMap;
}
}

View file

@ -0,0 +1,18 @@
/*******************************************************************************
* Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.parser.antlr;
import java.io.InputStream;
/**
* @author Jan Köhnlein - Initial contribution and API
*/
public interface IAntlrTokenFileProvider {
InputStream getAntlrTokenFile();
}