1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.dialogs.stoex; |
5 | |
6 | import java.lang.reflect.InvocationTargetException; |
7 | |
8 | import org.antlr.runtime.ANTLRStringStream; |
9 | import org.antlr.runtime.BaseRecognizer; |
10 | import org.antlr.runtime.CharStream; |
11 | import org.antlr.runtime.Lexer; |
12 | import org.eclipse.jface.text.BadLocationException; |
13 | import org.eclipse.jface.text.IDocument; |
14 | import org.eclipse.jface.text.rules.IToken; |
15 | import org.eclipse.jface.text.rules.ITokenScanner; |
16 | |
17 | /** |
18 | * @author Steffen Becker |
19 | * |
20 | */ |
21 | public class ANTLRTokenScannerAdapter implements ITokenScanner { |
22 | |
23 | int lastTokenLength = 0; |
24 | |
25 | private int tokenOffset; |
26 | |
27 | private Class<?> scannerClass; |
28 | |
29 | private Lexer scanner; |
30 | |
31 | private String currentInput; |
32 | |
33 | private IDocument currentDocument; |
34 | |
35 | private int baseOffset; |
36 | |
37 | private ITokenMapper myMapper; |
38 | |
39 | private int lastTokenOffset; |
40 | |
41 | /** |
42 | * |
43 | */ |
44 | public ANTLRTokenScannerAdapter(Class<?> scannerClass, ITokenMapper mapper) { |
45 | this.scannerClass = scannerClass; |
46 | this.myMapper = mapper; |
47 | } |
48 | |
49 | /* |
50 | * (non-Javadoc) |
51 | * |
52 | * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength() |
53 | */ |
54 | public int getTokenLength() { |
55 | return lastTokenLength; |
56 | } |
57 | |
58 | /* |
59 | * (non-Javadoc) |
60 | * |
61 | * @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset() |
62 | */ |
63 | public int getTokenOffset() { |
64 | return tokenOffset; |
65 | } |
66 | |
67 | /* |
68 | * (non-Javadoc) |
69 | * |
70 | * @see org.eclipse.jface.text.rules.ITokenScanner#nextToken() |
71 | */ |
72 | public IToken nextToken() { |
73 | tokenOffset = baseOffset + scanner.getCharIndex(); // Token starts at point where lexer is |
74 | ANTLRTokenWrapper wrapper = new ANTLRTokenWrapper(scanner.nextToken(), myMapper); |
75 | lastTokenLength = wrapper.getToken().getText() == null ? 0 : wrapper.getToken().getText().length(); |
76 | tokenOffset += (scanner.getCharIndex() + baseOffset) - tokenOffset - lastTokenLength; // Correct the position in case of recognition exceptions |
77 | |
78 | wrapper.setIsWhitespace(wrapper.getToken().getChannel() == BaseRecognizer.HIDDEN);// Token is a Whitespace |
79 | return wrapper; |
80 | } |
81 | |
82 | /* |
83 | * (non-Javadoc) |
84 | * |
85 | * @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, |
86 | * int, int) |
87 | */ |
88 | public void setRange(IDocument document, int offset, int length) { |
89 | currentInput = ""; currentDocument = document; |
90 | try { |
91 | currentInput = document.get(offset, length); |
92 | lastTokenOffset = 0; |
93 | tokenOffset = offset; |
94 | baseOffset = offset; |
95 | } catch (BadLocationException e1) { |
96 | // TODO Auto-generated catch block |
97 | e1.printStackTrace(); |
98 | } |
99 | ANTLRStringStream reader = new ANTLRStringStream(currentInput); |
100 | try { |
101 | scanner = (Lexer) scannerClass.getConstructor(CharStream.class) |
102 | .newInstance(new Object[] { reader }); |
103 | } catch (IllegalArgumentException e) { |
104 | // TODO Auto-generated catch block |
105 | e.printStackTrace(); |
106 | } catch (SecurityException e) { |
107 | // TODO Auto-generated catch block |
108 | e.printStackTrace(); |
109 | } catch (InstantiationException e) { |
110 | // TODO Auto-generated catch block |
111 | e.printStackTrace(); |
112 | } catch (IllegalAccessException e) { |
113 | // TODO Auto-generated catch block |
114 | e.printStackTrace(); |
115 | } catch (InvocationTargetException e) { |
116 | // TODO Auto-generated catch block |
117 | e.printStackTrace(); |
118 | } catch (NoSuchMethodException e) { |
119 | // TODO Auto-generated catch block |
120 | e.printStackTrace(); |
121 | } |
122 | } |
123 | |
124 | } |