1 | // Copyright (C) 2008 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package com.google.caja.plugin.stages; |
16 | |
17 | import com.google.caja.SomethingWidgyHappenedError; |
18 | import com.google.caja.lexer.FilePosition; |
19 | import com.google.caja.lexer.InputSource; |
20 | import com.google.caja.parser.ParseTreeNodeContainer; |
21 | import com.google.caja.parser.js.Expression; |
22 | import com.google.caja.parser.js.IntegerLiteral; |
23 | import com.google.caja.parser.js.StringLiteral; |
24 | import com.google.caja.reporting.MessageContext; |
25 | |
26 | import java.io.IOException; |
27 | import java.util.ArrayList; |
28 | import java.util.HashSet; |
29 | import java.util.LinkedHashMap; |
30 | import java.util.List; |
31 | import java.util.Map; |
32 | import java.util.Set; |
33 | |
34 | /** |
35 | * A module-specific collection of file positions of key pieces of cajoled code |
36 | * that can be bundled with a cajoled module. |
37 | * |
38 | * @author mikesamuel@gmail.com |
39 | */ |
40 | final class DebuggingSymbols { |
41 | private Map<FilePosition, Integer> positions |
42 | = new LinkedHashMap<FilePosition, Integer>(); |
43 | |
44 | /** Returns an index into the debugging symbols table. */ |
45 | public int indexForPosition(FilePosition pos) { |
46 | Integer index = positions.get(pos); |
47 | if (index == null) { |
48 | positions.put(pos, index = positions.size()); |
49 | } |
50 | return index; |
51 | } |
52 | |
53 | /** |
54 | * Produces a set of actuals that can be consumed by |
55 | * {@code ___.useDebugSymbols} from cajita-debugmode.js. |
56 | */ |
57 | public ParseTreeNodeContainer toJavascriptSideTable() { |
58 | MessageContext mc = new MessageContext(); |
59 | for (InputSource is : allInputSources()) { mc.addInputSource(is); } |
60 | List<Expression> debugTable = new ArrayList<Expression>( |
61 | positions.size() * 2 - 1); |
62 | String last = null; |
63 | for (FilePosition p : positions.keySet()) { |
64 | String posStr = formatPos(p, mc); |
65 | int prefixLen = 0; |
66 | if (last != null) { |
67 | prefixLen = commonPrefixLength(posStr, last); |
68 | debugTable.add(new IntegerLiteral(FilePosition.UNKNOWN, prefixLen)); |
69 | } |
70 | debugTable.add(StringLiteral.valueOf( |
71 | FilePosition.UNKNOWN, posStr.substring(prefixLen))); |
72 | last = posStr; |
73 | } |
74 | return new ParseTreeNodeContainer(debugTable); |
75 | } |
76 | |
77 | public boolean isEmpty() { return positions.isEmpty(); } |
78 | |
79 | private Set<InputSource> allInputSources() { |
80 | Set<InputSource> sources = new HashSet<InputSource>(); |
81 | for (FilePosition p : positions.keySet()) { |
82 | sources.add(p.source()); |
83 | } |
84 | return sources; |
85 | } |
86 | |
87 | /** Length of the longest string that is a prefix of both. */ |
88 | private static int commonPrefixLength(String a, String b) { |
89 | int n = Math.min(a.length(), b.length()); |
90 | int prefixLen = 0; |
91 | while (prefixLen < n && a.charAt(prefixLen) == b.charAt(prefixLen)) { |
92 | ++prefixLen; |
93 | } |
94 | return prefixLen; |
95 | } |
96 | |
97 | private static String formatPos(FilePosition pos, MessageContext mc) { |
98 | StringBuilder sb = new StringBuilder(); |
99 | try { |
100 | pos.format(mc, sb); |
101 | } catch (IOException ex) { |
102 | throw new SomethingWidgyHappenedError(ex); |
103 | } |
104 | return sb.toString(); |
105 | } |
106 | } |