[193] | 1 | package com.framsticks.framclipse.internal.parser; |
---|
| 2 | |
---|
| 3 | import java.io.File; |
---|
| 4 | import java.io.FileReader; |
---|
| 5 | import java.io.InputStream; |
---|
| 6 | import java.lang.reflect.Field; |
---|
| 7 | import java.net.URL; |
---|
| 8 | |
---|
| 9 | import com.framsticks.framclipse.editors.EditorType; |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | public class FramclipseParser { |
---|
| 13 | |
---|
| 14 | public final static void recursiveSetOffset(Node root,int begin,int end){ |
---|
| 15 | final int modelNumChild = root.jjtGetNumChildren(); |
---|
| 16 | for (int i = 0; i < modelNumChild; ++i) { |
---|
| 17 | final Node child = root.jjtGetChild(i); |
---|
| 18 | if (child instanceof ElementWithOffset) { |
---|
| 19 | final ElementWithOffset ewo = (ElementWithOffset) child; |
---|
| 20 | ewo.setBeginOffset(begin); |
---|
| 21 | ewo.setEndOffset(end); |
---|
| 22 | } |
---|
| 23 | recursiveSetOffset(child, begin, end); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | public Node parse(String content, EditorType editorType, URL url) throws ParseException { |
---|
| 29 | if (editorType.equals(EditorType.FRAMSCRIPT)) { |
---|
| 30 | |
---|
| 31 | } else { |
---|
| 32 | try { |
---|
| 33 | FramclipseNonScriptParser parser = new FramclipseNonScriptParser( |
---|
| 34 | content); |
---|
| 35 | ElementWithOffset model = parser.FramclipseFile(); |
---|
| 36 | model.setBeginOffset(0); |
---|
| 37 | model.setEndOffset(content.length() - 1); |
---|
| 38 | if (url == null) return model; |
---|
| 39 | String path = url.toString(); |
---|
| 40 | path = path.substring(0, path.lastIndexOf('/') + 1); |
---|
| 41 | // if (File.separatorChar != '/') |
---|
| 42 | // path = path.replace('/', File.separatorChar); |
---|
| 43 | |
---|
| 44 | final int modelNumChild = model.jjtGetNumChildren(); |
---|
| 45 | for (int i = 0; i < modelNumChild; ++i) { |
---|
| 46 | final Node child = model.jjtGetChild(i); |
---|
| 47 | if (child instanceof ASTGlobalInclude) { |
---|
| 48 | final ASTGlobalInclude inc = (ASTGlobalInclude) child; |
---|
| 49 | final String fname = inc.getFileName(); |
---|
| 50 | if (!"N/A".equals(fname)) { |
---|
| 51 | try { |
---|
| 52 | final URL incurl = new URL(path + fname); |
---|
| 53 | final InputStream stream = incurl.openStream(); |
---|
| 54 | final CharStreamWithOffset cs = new CharStreamWithOffset(stream); |
---|
| 55 | final FramclipseNonScriptParser parser2 = new FramclipseNonScriptParser( |
---|
| 56 | cs); |
---|
| 57 | final Field fld = FramclipseNonScriptParser.class.getDeclaredField("charStream"); |
---|
| 58 | fld.setAccessible(true); |
---|
| 59 | fld.set(parser2, cs); |
---|
| 60 | final ASTFramclipseFile include = parser2 |
---|
| 61 | .FramclipseFile(); |
---|
| 62 | for (int j = 0; j < include.jjtGetNumChildren(); ++j) { |
---|
| 63 | final Node ic = include.jjtGetChild(j); |
---|
| 64 | inc.jjtAddChild(ic, j); |
---|
| 65 | ic.jjtSetParent(inc); |
---|
| 66 | } |
---|
| 67 | recursiveSetOffset(inc, inc.getBeginOffset(), inc.getEndOffset()); |
---|
| 68 | } catch (ParseException e) { |
---|
| 69 | inc.setValid(false); |
---|
| 70 | System.err.println("In "+path+fname); |
---|
| 71 | e.printStackTrace(); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | return model; |
---|
| 77 | } catch (ParseException e) { |
---|
| 78 | e.printStackTrace(); |
---|
| 79 | throw e; |
---|
| 80 | } catch (TokenMgrError e) { |
---|
| 81 | e.printStackTrace(); |
---|
| 82 | throw e; |
---|
| 83 | } catch (Exception e) { |
---|
| 84 | e.printStackTrace(); |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | return null; |
---|
| 89 | |
---|
| 90 | } |
---|
| 91 | } |
---|