| 1 | package org.axdt.as3.imp.parser; |
|---|
| 2 | |
|---|
| 3 | import org.axdt.as3.AS3Plugin; |
|---|
| 4 | import org.axdt.as3.imp.parser.Ast.ASTNode; |
|---|
| 5 | import org.eclipse.core.runtime.IPath; |
|---|
| 6 | import org.eclipse.core.runtime.IProgressMonitor; |
|---|
| 7 | import org.eclipse.imp.services.ILanguageSyntaxProperties; |
|---|
| 8 | import org.eclipse.imp.model.ISourceProject; |
|---|
| 9 | import org.eclipse.imp.parser.ILexer; |
|---|
| 10 | import org.eclipse.imp.parser.IMessageHandler; |
|---|
| 11 | import org.eclipse.imp.parser.IParseController; |
|---|
| 12 | import org.eclipse.imp.parser.IParser; |
|---|
| 13 | import org.eclipse.imp.parser.ISourcePositionLocator; |
|---|
| 14 | import org.eclipse.imp.parser.MessageHandlerAdapter; |
|---|
| 15 | import org.eclipse.imp.parser.SimpleLPGParseController; |
|---|
| 16 | |
|---|
| 17 | public class AS3ParseController extends SimpleLPGParseController implements IParseController { |
|---|
| 18 | private AS3Parser parser; |
|---|
| 19 | |
|---|
| 20 | private AS3Lexer lexer; |
|---|
| 21 | |
|---|
| 22 | private Object backupAst; |
|---|
| 23 | |
|---|
| 24 | public AS3ParseController() { |
|---|
| 25 | super(AS3Plugin.LANGUAGE); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * @param filePath |
|---|
| 30 | * Absolute path of file |
|---|
| 31 | * @param project |
|---|
| 32 | * Project that contains the file |
|---|
| 33 | * @param handler |
|---|
| 34 | * A message handler to receive error messages (or any others) |
|---|
| 35 | * from the parser |
|---|
| 36 | */ |
|---|
| 37 | public void initialize(IPath filePath, ISourceProject project, IMessageHandler handler) { |
|---|
| 38 | super.initialize(filePath, project, handler); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | public IParser getParser() { |
|---|
| 42 | return parser; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | public ILexer getLexer() { |
|---|
| 46 | return lexer; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | public ISourcePositionLocator getNodeLocator() { |
|---|
| 50 | return new AS3ASTNodeLocator(); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | public ILanguageSyntaxProperties getSyntaxProperties() { |
|---|
| 54 | return new AS3SyntaxProperties(); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | public Object parse(String contents, IProgressMonitor monitor) { |
|---|
| 59 | return parse(contents, false, monitor); |
|---|
| 60 | } |
|---|
| 61 | /** |
|---|
| 62 | * setFilePath() should be called before calling this method. |
|---|
| 63 | */ |
|---|
| 64 | public Object parse(String contents, boolean scanOnly, IProgressMonitor monitor) { |
|---|
| 65 | PMMonitor my_monitor = new PMMonitor(monitor); |
|---|
| 66 | char[] contentsArray = contents.toCharArray(); |
|---|
| 67 | |
|---|
| 68 | if (lexer == null) { |
|---|
| 69 | lexer = new AS3Lexer(); |
|---|
| 70 | } |
|---|
| 71 | lexer.reset(contentsArray, fFilePath.toPortableString()); |
|---|
| 72 | |
|---|
| 73 | if (parser == null) { |
|---|
| 74 | parser = new AS3Parser(lexer.getILexStream()); |
|---|
| 75 | } |
|---|
| 76 | parser.reset(lexer.getILexStream()); |
|---|
| 77 | parser.getIPrsStream().setMessageHandler(new AS3MessageHandler(handler)); |
|---|
| 78 | |
|---|
| 79 | // Lex the stream to produce the token stream |
|---|
| 80 | lexer.lexer(my_monitor, parser, true); |
|---|
| 81 | // fCurrentAst might (probably will) be |
|---|
| 82 | // inconsistent wrt the lex stream now |
|---|
| 83 | if (my_monitor.isCancelled()) return fCurrentAst; |
|---|
| 84 | |
|---|
| 85 | if (fCurrentAst != null) |
|---|
| 86 | backupAst = fCurrentAst; |
|---|
| 87 | |
|---|
| 88 | fCurrentAst = parser.parser(my_monitor, -1); |
|---|
| 89 | |
|---|
| 90 | if (fCurrentAst instanceof ASTNode) { |
|---|
| 91 | parser.resolve((ASTNode) fCurrentAst); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | cacheKeywordsOnce(); |
|---|
| 95 | |
|---|
| 96 | Object result = fCurrentAst; |
|---|
| 97 | return result; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | public Object getBackupAst() { |
|---|
| 101 | return backupAst; |
|---|
| 102 | } |
|---|
| 103 | } |
|---|