JFlexXref.java revision 1470
850N/A/*
850N/A * CDDL HEADER START
850N/A *
850N/A * The contents of this file are subject to the terms of the
850N/A * Common Development and Distribution License (the "License").
850N/A * You may not use this file except in compliance with the License.
850N/A *
850N/A * See LICENSE.txt included in this distribution for the specific
850N/A * language governing permissions and limitations under the License.
850N/A *
850N/A * When distributing Covered Code, include this CDDL HEADER in each
850N/A * file and include the License file at LICENSE.txt.
850N/A * If applicable, add the following below this CDDL HEADER, with the
850N/A * fields enclosed by brackets "[]" replaced with your own identifying
850N/A * information: Portions Copyright [yyyy] [name of copyright owner]
850N/A *
850N/A * CDDL HEADER END
850N/A */
850N/A
850N/A/*
1259N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
1185N/A * Portions Copyright 2011, 2012 Jens Elkner.
850N/A */
850N/A
850N/Apackage org.opensolaris.opengrok.analysis;
850N/A
1058N/Aimport java.io.CharArrayReader;
850N/Aimport java.io.IOException;
1058N/Aimport java.io.Reader;
850N/Aimport java.lang.reflect.Field;
1020N/Aimport java.util.ArrayDeque;
1145N/Aimport java.util.ArrayList;
1145N/Aimport java.util.Comparator;
1145N/Aimport java.util.HashMap;
943N/Aimport java.util.Map;
1145N/Aimport java.util.Set;
1439N/Aimport java.util.SortedSet;
1145N/Aimport java.util.TreeSet;
1145N/Aimport java.util.logging.Logger;
857N/A
850N/Aimport org.opensolaris.opengrok.analysis.Definitions.Tag;
850N/Aimport org.opensolaris.opengrok.configuration.Configuration;
1020N/Aimport org.opensolaris.opengrok.configuration.Project;
850N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
850N/Aimport org.opensolaris.opengrok.history.Annotation;
1020N/Aimport org.opensolaris.opengrok.web.Util;
850N/A
850N/A/**
850N/A * Base class for Xref lexers.
1020N/A *
1121N/A * @author Lubos Kosco
1121N/A */
1121N/Apublic abstract class JFlexXref {
1121N/A private static final Logger logger = Logger.getLogger(JFlexXref.class.getName());
1121N/A /** Where to write xref content. */
1121N/A public XrefWriter out;
1121N/A /** URL prefix to use when generating search URLs. Per default this is
1185N/A * {@link Configuration#getUrlPrefix()}.*/
1190N/A public String urlPrefix = RuntimeEnvironment.getConfig().getUrlPrefix();
1185N/A /** Annotation to use when writing out annotation info.*/
1185N/A public Annotation annotation;
1185N/A /** The project, to which this instance is related to. */
1190N/A public Project project;
1190N/A /** symbol definitions to use when writing out related info */
1185N/A protected Definitions defs;
1185N/A /**
1185N/A * A stack of <span ...> CSS class names currently opened.
1020N/A * It is used to close all open spans for a line and re-open them at the
1439N/A * start of the next line to produce wellformed XML. So if a
1439N/A * <span ...> is opened and do not closed on the same line, the class
1439N/A * name used needs to be pushed onto this stack. If the <span ...>
1145N/A * gets closed on a different line, it needs to be popped from this stack.
1259N/A * If properly done, the stack should be empty when the last line has been
1145N/A * written.
1259N/A */
1259N/A @SuppressWarnings("serial")
1259N/A protected class SpanStack extends ArrayList<String> {
1259N/A /**
1259N/A * Create a new empty instance with the default capacity.
1259N/A */
1259N/A public SpanStack() {
1259N/A super();
1259N/A }
1259N/A /**
1259N/A * The preferred method to add a new span class name to this instance.
1259N/A * @param s class name to add.
1259N/A * @see #pop()
1259N/A * @throws IllegalArgumentException if the given argument is {@code null}.
1259N/A */
1259N/A public void push(String s) {
1259N/A if (s == null) {
1259N/A throw new IllegalArgumentException("null is not allowed");
1259N/A }
1259N/A super.add(s);
1259N/A }
1259N/A
1259N/A /**
1259N/A * The preferred method to remove the last element from this stack.
1259N/A * @return {@code null} if the stack is empty, the removed element
1259N/A * otherwise.
1259N/A * @see #push(String)
1259N/A */
1259N/A public String pop() {
1259N/A return isEmpty() ? null : remove(size() - 1);
1259N/A }
1259N/A }
1259N/A
1259N/A /** the {@link SpanStack} for this and only this instance */
1259N/A protected final SpanStack spans;
1259N/A /** EOF value returned by yylex(). */
1259N/A private final int yyeof;
1259N/A /** See {@link Configuration#getUserPage()}. Per default initialized
1259N/A * in the constructor and here to be consistent and avoid lot of
1259N/A * unnecessary lookups.
1259N/A * @see #startNewLine() */
1259N/A protected String userPageLink;
1259N/A /** See {@link Configuration#getUserPageSuffix()}. Per default
1259N/A * initialized in the constructor and here to be consistent and avoid lot of
1145N/A * unnecessary lookups.
1145N/A * @see #startNewLine() */
1121N/A protected String userPageSuffix;
1121N/A
1121N/A /** Stack to remember the order of relevant states for the current parser.
1121N/A * @see #labelStack */
1121N/A protected ArrayDeque<Integer> stateStack = new ArrayDeque<Integer>();
1121N/A /** Stack to remember the order of relevant state related labels for the
1121N/A * current parser. To keep consistency, {@link #stateStack} opertions should
1185N/A * always performed accordingly to this instance (i.e. if you wanna pop,
1185N/A * pop both, etc. ;-))! */
1185N/A protected SpanStack labelStack = new SpanStack();
1185N/A
1185N/A /**
1185N/A * Description of the style to use for a type of definitions.
1185N/A */
1185N/A private static class Style {
1121N/A /** Name of the style definition as given by CTags. */
1121N/A final String name;
1121N/A
1121N/A /** Class name used by the style sheets when rendering the xref. */
1121N/A final String ssClass;
1121N/A
1121N/A /**
1121N/A * The title of the section to which this type belongs, or {@code null}
1121N/A * if this type should not be listed in the navigation panel.
1121N/A */
1121N/A final String title;
1020N/A
1121N/A /** Construct a style description. */
1121N/A Style(String name, String ssClass, String title) {
1121N/A this.name = name;
1121N/A this.ssClass = ssClass;
1121N/A this.title = title;
1121N/A }
1121N/A }
1121N/A
1121N/A /**
1121N/A * Description of styles to use for different types of definitions.
1121N/A */
1121N/A private static final Style[] DEFINITION_STYLES = {
1121N/A new Style("macro", "xm", "Macro"),
1121N/A new Style("argument", "xa", null),
1121N/A new Style("local", "xl", null),
1121N/A new Style("variable", "xv", "Variable"),
1121N/A new Style("class", "xc", "Class"),
1121N/A new Style("package", "xp", "Package"),
1121N/A new Style("interface", "xi", "Interface"),
1121N/A new Style("namespace", "xn", "Namespace"),
1121N/A new Style("enumerator", "xer", null),
1121N/A new Style("enum", "xe", "Enum"),
1121N/A new Style("struct", "xs", "Struct"),
1121N/A new Style("typedefs", "xts", null),
1121N/A new Style("typedef", "xt", "Typedef"),
1058N/A new Style("union", "xu", null),
1121N/A new Style("field", "xfld", null),
1121N/A new Style("member", "xmb", null),
1121N/A new Style("function", "xf", "Function"),
1121N/A new Style("method", "xmt", "Method"),
1121N/A new Style("subroutine", "xsr", "Subroutine"),
1121N/A };
1121N/A
1121N/A /**
1121N/A * Create a new lexer instance. Initializes {@link #userPageLink},
1121N/A * {@link #userPageSuffix} using the runtime environment and creates a new
1121N/A * empty {@link SpanStack}.
850N/A * @see Configuration#getUserPage()
1439N/A * @see Configuration#getUserPageSuffix()
1439N/A */
1439N/A protected JFlexXref() {
1439N/A try {
1121N/A // TODO when bug #16053 is fixed, we should add a getter to a file
1121N/A // that's included from all the Xref classes so that we avoid the
1121N/A // reflection.
1121N/A Field f = getClass().getField("YYEOF");
1121N/A yyeof = f.getInt(null);
1121N/A userPageLink = RuntimeEnvironment.getConfig().getUserPage();
1121N/A if (userPageLink != null && userPageLink.length() == 0) {
1121N/A userPageLink = null;
1145N/A }
1121N/A userPageSuffix = RuntimeEnvironment.getConfig().getUserPageSuffix();
1121N/A if (userPageSuffix != null && userPageSuffix.length() == 0) {
1121N/A userPageSuffix = null;
1121N/A }
1121N/A } catch (Exception e) {
1121N/A // The auto-generated constructors for the Xref classes don't
1020N/A // expect a checked exception, so wrap it in an AssertionError.
1121N/A // This should never happen, since all the Xref classes will get
1145N/A // a public static YYEOF field from JFlex.
1145N/A AssertionError ae = new AssertionError("Couldn't initialize yyeof");
1145N/A ae.initCause(e);
1145N/A throw ae; // NOPMD (stack trace is preserved by initCause(), but
1145N/A // PMD thinks it's lost)
1145N/A }
1145N/A spans = new SpanStack();
1145N/A }
1145N/A
1145N/A /**
1145N/A * Reinitialize the xref with new contents.
1145N/A *
1145N/A * @param contents a char buffer with text to analyze
1145N/A * @param length the number of characters to use from the char buffer
1145N/A */
1145N/A public void reInit(char[] contents, int length) {
1145N/A yyreset(new CharArrayReader(contents, 0, length));
1145N/A annotation = null;
1145N/A }
1145N/A
1145N/A /**
1145N/A * Set the symbol defintions to use when writing out related info.
1145N/A * @param defs symbol definitions to use. Might be {@code null}.
1145N/A */
1145N/A public void setDefs(Definitions defs) {
1145N/A this.defs = defs;
1145N/A }
1145N/A
1145N/A /**
1145N/A * Write out the URL parameter, which specifies the project to use. Does
1259N/A * nothing if {@link #project} is not set.
1259N/A * @throws IOException
1259N/A */
1145N/A protected void appendProject() throws IOException {
1145N/A if (project != null) {
1259N/A out.write("&amp;project=");
1145N/A out.write(project.getDescription());
1145N/A }
1145N/A }
1145N/A
1145N/A /**
1145N/A * Get the URL parameter, which specifies the project to use.
1145N/A * @return an empty String if {@link #project} is not set, the parameter
1145N/A * including the leading &amp; otherwise.
1145N/A */
1259N/A protected String getProjectPostfix() {
1259N/A return project == null ? "" : ("&amp;project=" + project.getDescription());
1145N/A }
1145N/A
1145N/A /**
1145N/A * Run the scanner to get the next token from the input.
1145N/A * @return {@code true} if a new Token is available/was found.
1259N/A * @throws IOException */
1145N/A public abstract int yylex() throws IOException;
1259N/A
1145N/A /**
1145N/A * Closes the current input stream, and resets the scanner to read from the
1145N/A * given input stream. All internal variables are reset, the old input
1145N/A * stream cannot be reused (content of the internal buffer is discarded and
1145N/A * lost). The lexical state is set to {@code YY_INITIAL}.
1145N/A * @param reader the new input stream to operate on.*/
1145N/A public abstract void yyreset(Reader reader);
1145N/A
1145N/A /**
1145N/A * Get the number of the current line of input (which is usually the same as
1145N/A * {@code yyline}).
1145N/A * @return a value &gt;= 0.
1145N/A */
1145N/A protected abstract int getLineNumber();
1145N/A
1145N/A /**
1145N/A * Set the number of the current line of input (which is usually the same as
1145N/A * {@code yyline}).
1185N/A * @param line line number to set. */
1190N/A protected abstract void setLineNumber(int line);
1145N/A
1145N/A /**
1145N/A * Enter the given lexical state.
1145N/A * @param newState state to enter
1145N/A */
1145N/A public abstract void yybegin(int newState);
1259N/A
1259N/A /**
1145N/A * Get the current lexical state of the scanner.
1145N/A * @return a lexical state.
1259N/A */
1259N/A public abstract int yystate();
1259N/A
1145N/A /**
1145N/A * Write the crossfile content to the specified {@code Writer}.
1145N/A *
1259N/A * @param out xref destination
1145N/A * @throws IOException on error when writing the xref
1145N/A */
1145N/A public void write(XrefWriter out) throws IOException {
1121N/A this.out = out;
1121N/A writeSymbolTable();
1121N/A setLineNumber(0);
1121N/A out.write("<div id='lines'\n>");
1121N/A startNewLine();
1121N/A while (yylex() != yyeof) { // NOPMD while statement intentionally empty
1121N/A // nothing to do here, yylex() will do the work
1121N/A }
1185N/A finishLine();
1121N/A out.write("</div\n>");
1020N/A out.setLines(getLineNumber());
1121N/A if (spans.size() != 0) {
1121N/A logger.info("The SpanStack for " + out.getFile()
1121N/A + " is not empty! May be the " + getClass().getSimpleName()
1121N/A + " lexer is not perfect yet!");
1121N/A spans.clear();
1121N/A }
1121N/A }
1121N/A
1121N/A /**
1121N/A * Write a JavaScript function that returns an array with the definitions
1121N/A * to list in the navigation panel. Each element of the array is itself an
1121N/A * array containing the name of the definition type, the CSS class name for
1121N/A * the type, and an array of (symbol, line) pairs for the definitions of
1020N/A * that type.
1121N/A */
1121N/A private void writeSymbolTable() throws IOException {
1121N/A if (defs == null) {
1108N/A // No definitions, no symbol table to write
1121N/A return;
1121N/A }
1121N/A
1121N/A // We want the symbol table to be sorted
943N/A Comparator<Tag> cmp = new Comparator<Tag>() {
1259N/A @Override
1145N/A public int compare(Tag tag1, Tag tag2) {
1259N/A // Order by symbol name, and then by line number if multiple
1121N/A // definitions use the same symbol name
1121N/A int ret = tag1.symbol.compareTo(tag2.symbol);
1121N/A if (ret == 0) {
1121N/A ret = tag1.line - tag2.line;
1121N/A }
1121N/A return ret;
1121N/A }
1121N/A };
1121N/A
1121N/A Map<String, SortedSet<Tag>> symbols =
1121N/A new HashMap<String, SortedSet<Tag>>();
1108N/A
1121N/A for (Tag tag : defs.getTags()) {
1121N/A Style style = getStyle(tag.type);
1121N/A if (style != null && style.title != null) {
1121N/A SortedSet<Tag> tags = symbols.get(style.name);
1121N/A if (tags == null) {
1121N/A tags = new TreeSet<Tag>(cmp);
1121N/A symbols.put(style.name, tags);
1121N/A }
1121N/A tags.add(tag);
1121N/A }
1121N/A }
1121N/A
1121N/A out.append("<script type=\"text/javascript\">/* <![CDATA[ */\n");
1121N/A out.append("O.symlist = [");
1121N/A
1121N/A boolean first = true;
1121N/A for (Style style : DEFINITION_STYLES) {
1121N/A SortedSet<Tag> tags = symbols.get(style.name);
1121N/A if (tags != null) {
1121N/A if (!first) {
1121N/A out.append(',');
1121N/A }
1121N/A out.append("[\"");
1121N/A out.append(style.title);
943N/A out.append("\",\"");
1121N/A out.append(style.ssClass);
1121N/A out.append("\",[");
1121N/A
1121N/A boolean firstTag = true;
1121N/A for (Tag tag : tags) {
1121N/A if (!firstTag) {
1121N/A out.append(',');
1121N/A }
1121N/A out.append('[');
1121N/A out.append(Util.jsStringLiteral(tag.symbol));
1121N/A out.append(',');
1121N/A out.append(Integer.toString(tag.line));
1121N/A out.append(']');
1121N/A firstTag = false;
1121N/A }
943N/A out.append("]]");
1121N/A first = false;
1121N/A }
1121N/A }
1121N/A /* no LF intentionally - xml is whitespace aware ... */
1121N/A out.append("]; /* ]]> */</script>");
1121N/A }
1121N/A
1121N/A /**
1121N/A * Get the style description for a definition type.
1185N/A *
1121N/A * @param type the definition type
1121N/A * @return the style of a definition type, or {@code null} if no style is
1122N/A * defined for the type
1122N/A * @see #DEFINITION_STYLES
1123N/A */
1123N/A private static Style getStyle(String type) {
1123N/A for (Style style : DEFINITION_STYLES) {
1122N/A if (type.startsWith(style.name)) {
1122N/A return style;
1122N/A }
1122N/A }
1122N/A return null;
1123N/A }
1123N/A
1123N/A /**
1123N/A * Write out annotation infos for the given line.
1123N/A *
1122N/A * @param num linenumber to print
1439N/A * @throws IOException depends on the destination (<var>out</var>).
1439N/A */
1439N/A private final void writeAnnotationInfos(int num) throws IOException {
1439N/A String r = annotation.getRevision(num);
1439N/A out.write("<span class='blame'>");
1439N/A out.write("<a class='r' href=\"");
1439N/A out.write(Util.uriEncodePath(annotation.getFilename()));
1439N/A out.write("?a=true&amp;r=");
1439N/A out.write(r);
1439N/A String msg = annotation.getDesc(r);
1439N/A if (msg != null) {
1439N/A out.write("\" title=\"");
1439N/A out.write(Util.formQuoteEscape(msg));
1439N/A }
850N/A out.write("\">");
out.write(r);
out.write("</a>");
String a = annotation.getAuthor(num);
if (userPageLink == null) {
out.write("<span class='a'>");
out.write(Util.htmlize(a));
out.write("</span>");
} else {
out.write("<a class='a' href=\"");
out.write(userPageLink);
out.write(Util.uriEncodePath(a));
if (userPageSuffix != null) {
out.write(userPageSuffix);
}
out.write("\">");
out.write(Util.htmlize(a));
out.write("</a>");
}
out.write("</span>");
}
private final void finishLine() throws IOException {
if (out.getMark() == out.getCount()) {
out.write(' '); // <div></div> doesn't produce a line-height block
}
if (spans.size() != 0) {
for (int i=spans.size()-1; i >= 0; i--) {
out.write("</span>");
}
}
out.write("</div\n>");
}
/**
* Terminate the current line and insert preamble for the next line. The
* line count will be incremented.
*
* @throws IOException on error when writing the xref
*/
protected final void startNewLine() throws IOException {
int line = getLineNumber();
if (line != 0) {
finishLine();
}
setLineNumber(++line);
/* <div id="N">...</div>
* -> uncompressed size ~6%, compressed size ~26% bigger;
* <div id="N"><b>N</b>...</div>
* -> uncompressed size ~12%, compressed size ~52% bigger;
* <span class="[h]l" id="N">N</span>...
* -> is about the same as previous one
*/
out.write("<div>");
if (annotation != null) {
writeAnnotationInfos(line);
}
if (spans.size() != 0) {
for (String cname : spans) {
if (cname.isEmpty()) {
out.write("<span>");
} else {
out.write("<span class='");
out.write(cname);
out.write("'>");
}
}
}
out.mark();
}
/**
* Write a symbol and generate links as appropriate.
*
* @param symbol the symbol to write.
* @param keywords a set of keywords recognized by this analyzer (no links
* will be generated if the symbol is a keyword)
* @param line the line number on which the symbol appears
* @param escape If {@code true} <var>symbol</var> gets escapes for URI paths
* and html text. Otherwise it is assumed, that symbol contains no HTML
* special characters.
* @throws IOException if an error occurs while writing to the stream
*/
protected void writeSymbol(String symbol, Set<String> keywords, int line,
boolean escape) throws IOException
{
String[] strs = new String[1];
strs[0] = "";
if (keywords != null && keywords.contains(symbol)) {
String htmlSymbol = escape ? Util.htmlize(symbol) : symbol;
// This is a keyword, so we don't create a link.
out.append("<b>").append(htmlSymbol).append("</b>");
} else if (defs != null && defs.hasDefinitionAt(symbol, line, strs)) {
// This is the definition of the symbol.
String type = strs[0];
String style_class = "d";
Style style = getStyle(type);
if (style != null) {
style_class = style.ssClass;
}
// 1) Create an anchor for direct links. (Perhaps we should only
// do this when there's exactly one definition of the symbol in
// this file? Otherwise, we may end up with multiple anchors with
// the same name.)
out.append("<a class=\"");
out.append(style_class);
out.append("\" name=\"");
out.append(escape ? Util.formQuoteEscape(symbol) : symbol);
out.append("\"/>");
// 2) Create a link that searches for all references to this symbol.
out.append("<a href=\"");
out.append(urlPrefix);
out.append("refs=");
out.append(escape ? Util.uriEncodeQueryValue(symbol) : symbol);
appendProject();
out.append("\" class=\"");
out.append(style_class);
out.append("\">");
out.append(escape ? Util.htmlize(symbol) : symbol);
out.append("</a>");
} else if (defs != null && defs.occurrences(symbol) == 1) {
// This is a reference to a symbol defined exactly once in this file.
String style_class = "d";
// Generate a direct link to the symbol definition.
out.append("<a class=\"");
out.append(style_class);
out.append("\" href=\"#");
out.append(escape ? Util.uriEncodeQueryValue(symbol) : symbol);
out.append("\">");
out.append(escape ? Util.htmlize(symbol) : symbol);
out.append("</a>");
} else {
// This is a symbol that is not defined in this file, or a symbol
// that is defined more than once in this file. In either case, we
// can't generate a direct link to the definition, so generate a
// link to search for all definitions of that symbol instead.
out.append("<a href=\"");
out.append(urlPrefix);
out.append("defs=");
out.append(escape ? Util.uriEncodeQueryValue(symbol) : symbol);
appendProject();
out.append("\">");
out.append(escape ? Util.htmlize(symbol) : symbol);
out.append("</a>");
}
}
/**
* Write out the Unicode character, unless it's an ISO control character
* &lt; 0x20, in which case it is ignored.
*
* @param c the character to write
* @throws IOException if an error occurs while writing to the stream
*/
protected void writeUnicodeChar(char c) throws IOException {
if (c > 0x20) {
out.append(c);
}
}
/**
* Write an e-mail address. The address will be obfuscated if
* {@link Configuration#isObfuscatingEMailAddresses()} returns
* {@code true}. It gets not htmlized because it is assumed to contain no
* HTML special characters.
*
* @param address the address to write
* @throws IOException if an error occurs while writing to the stream
*/
protected void writeEMailAddress(String address) throws IOException {
if (RuntimeEnvironment.getConfig().isObfuscatingEMailAddresses()) {
out.write(address.replace("@", " (at) "));
} else {
out.write(address);
}
}
/**
* Push the current state to the state order stack, the given label to the
* {@link #labelStack} and enter the given state.
* @param newState new state to enter.
* @param label label to push to the {@link #labelStack}. Might be {@code null}.
* @see #stateStack
* @see #yystate()
* @see #yybegin(int)
*/
@SuppressWarnings("boxing")
public void yypush(int newState, String label) {
stateStack.push(yystate());
labelStack.push(label);
yybegin(newState);
}
/**
* Pop the last entry from the state order stack and enter it. Also pop the
* the last label from the {@link #labelStack} and write it out, if it is
* not {@code null}.
* @throws IOException
* @see #stateStack
* @see #yybegin(int)
*/
@SuppressWarnings("boxing")
public void yypop() throws IOException {
yybegin(stateStack.pop());
out.write(labelStack.pop());
}
}