comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SLNodeFactory.java @ 16687:7c8ddb4233cd

SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Mon, 04 Aug 2014 18:53:21 -0700
parents 888907296590
children 2a5ec181dad4
comparison
equal deleted inserted replaced
16686:a59d447dde94 16687:7c8ddb4233cd
63 /* State while parsing a source unit. */ 63 /* State while parsing a source unit. */
64 private final SLContext context; 64 private final SLContext context;
65 private final Source source; 65 private final Source source;
66 66
67 /* State while parsing a function. */ 67 /* State while parsing a function. */
68 private int functionStartPos;
68 private String functionName; 69 private String functionName;
70 private int functionBodyStartPos; // includes parameter list
69 private int parameterCount; 71 private int parameterCount;
70 private FrameDescriptor frameDescriptor; 72 private FrameDescriptor frameDescriptor;
71 private List<SLStatementNode> methodNodes; 73 private List<SLStatementNode> methodNodes;
72 74
73 /* State while parsing a block. */ 75 /* State while parsing a block. */
79 this.context = context; 81 this.context = context;
80 this.source = source; 82 this.source = source;
81 this.prober = prober; 83 this.prober = prober;
82 } 84 }
83 85
84 public void startFunction(Token nameToken) { 86 public void startFunction(Token nameToken, int bodyStartPos) {
87 assert functionStartPos == 0;
85 assert functionName == null; 88 assert functionName == null;
89 assert functionBodyStartPos == 0;
86 assert parameterCount == 0; 90 assert parameterCount == 0;
87 assert frameDescriptor == null; 91 assert frameDescriptor == null;
88 assert lexicalScope == null; 92 assert lexicalScope == null;
89 93
94 functionStartPos = nameToken.charPos;
90 functionName = nameToken.val; 95 functionName = nameToken.val;
96 functionBodyStartPos = bodyStartPos;
91 frameDescriptor = new FrameDescriptor(); 97 frameDescriptor = new FrameDescriptor();
92 methodNodes = new ArrayList<>(); 98 methodNodes = new ArrayList<>();
93 startBlock(); 99 startBlock();
94 } 100 }
95 101
98 * Method parameters are assigned to local variables at the beginning of the method. This 104 * Method parameters are assigned to local variables at the beginning of the method. This
99 * ensures that accesses to parameters are specialized the same way as local variables are 105 * ensures that accesses to parameters are specialized the same way as local variables are
100 * specialized. 106 * specialized.
101 */ 107 */
102 final SourceSection src = srcFromToken(nameToken); 108 final SourceSection src = srcFromToken(nameToken);
103 SLReadArgumentNode readArg = new SLReadArgumentNode(src, parameterCount); 109 final SLReadArgumentNode readArg = new SLReadArgumentNode(src, parameterCount);
104 methodNodes.add(createAssignment(nameToken, readArg)); 110 methodNodes.add(createAssignment(nameToken, readArg));
105 parameterCount++; 111 parameterCount++;
106 } 112 }
107 113
108 public void finishFunction(SLStatementNode bodyNode) { 114 public void finishFunction(SLStatementNode bodyNode) {
109 methodNodes.add(bodyNode); 115 methodNodes.add(bodyNode);
110 // TODO (mlvdv) testing 116 final int bodyEndPos = bodyNode.getSourceSection().getCharEndIndex();
111 SLStatementNode methodBlock = finishBlock(methodNodes, -1, -1); 117 final SourceSection functionSrc = source.createSection(functionName, functionStartPos, bodyEndPos - functionStartPos);
118 final SLStatementNode methodBlock = finishBlock(methodNodes, functionBodyStartPos, bodyEndPos - functionBodyStartPos);
112 assert lexicalScope == null : "Wrong scoping of blocks in parser"; 119 assert lexicalScope == null : "Wrong scoping of blocks in parser";
113 120
114 SLFunctionBodyNode functionBodyNode = new SLFunctionBodyNode(methodBlock); 121 final SLFunctionBodyNode functionBodyNode = new SLFunctionBodyNode(functionSrc, methodBlock);
115 SLRootNode rootNode = new SLRootNode(frameDescriptor, functionBodyNode, functionName); 122 final SLRootNode rootNode = new SLRootNode(frameDescriptor, functionBodyNode, functionName);
116 123
117 context.getFunctionRegistry().register(functionName, rootNode); 124 context.getFunctionRegistry().register(functionName, rootNode);
118 125
126 functionStartPos = 0;
119 functionName = null; 127 functionName = null;
128 functionBodyStartPos = 0;
120 parameterCount = 0; 129 parameterCount = 0;
121 frameDescriptor = null; 130 frameDescriptor = null;
122 lexicalScope = null; 131 lexicalScope = null;
123 } 132 }
124 133
125 public void startBlock() { 134 public void startBlock() {
126 lexicalScope = new LexicalScope(lexicalScope); 135 lexicalScope = new LexicalScope(lexicalScope);
127 } 136 }
128 137
129 public SLStatementNode finishBlock(List<SLStatementNode> bodyNodes, int lBracePos, int length) { 138 public SLStatementNode finishBlock(List<SLStatementNode> bodyNodes, int startPos, int length) {
130 lexicalScope = lexicalScope.outer; 139 lexicalScope = lexicalScope.outer;
131 140
132 List<SLStatementNode> flattenedNodes = new ArrayList<>(bodyNodes.size()); 141 List<SLStatementNode> flattenedNodes = new ArrayList<>(bodyNodes.size());
133 flattenBlocks(bodyNodes, flattenedNodes); 142 flattenBlocks(bodyNodes, flattenedNodes);
134 143
135 if (lBracePos >= 0) { 144 final SourceSection src = source.createSection("block", startPos, length);
136 final SourceSection src = source.createSection("block", lBracePos, length); 145 return new SLBlockNode(src, flattenedNodes.toArray(new SLStatementNode[flattenedNodes.size()]));
137 return new SLBlockNode(src, flattenedNodes.toArray(new SLStatementNode[flattenedNodes.size()]));
138 }
139 if (flattenedNodes.size() == 0) {
140 // TODO (mlvdv) for error reporting, should have the character position, even if the
141 // block is empty.
142 return new SLBlockNode(null, new SLStatementNode[0]);
143 }
144 if (flattenedNodes.size() == 1) {
145 /*
146 * A block containing one other node, not surrounded by braces is unnecessary, we can
147 * just that other node.
148 */
149 return flattenedNodes.get(0);
150 }
151 /*
152 * A "block" not surrounded by braces.
153 */
154 final int start = flattenedNodes.get(0).getSourceSection().getCharIndex();
155 final int end = flattenedNodes.get(flattenedNodes.size() - 1).getSourceSection().getCharEndIndex();
156 return new SLBlockNode(source.createSection("block", start, end - start), flattenedNodes.toArray(new SLStatementNode[flattenedNodes.size()]));
157 } 146 }
158 147
159 private void flattenBlocks(Iterable<? extends Node> bodyNodes, List<SLStatementNode> flattenedNodes) { 148 private void flattenBlocks(Iterable<? extends Node> bodyNodes, List<SLStatementNode> flattenedNodes) {
160 for (Node n : bodyNodes) { 149 for (Node n : bodyNodes) {
161 if (n instanceof SLBlockNode) { 150 if (n instanceof SLBlockNode) {