diff options
author | Alexander Kurtakov | 2014-04-08 19:41:50 +0000 |
---|---|---|
committer | Alexander Kurtakov | 2014-04-08 20:24:33 +0000 |
commit | 45e91c272ac6331915bcb12c00f6799062fa7847 (patch) | |
tree | 0070510e10dfb478a0e39f2262a67c3b74807c48 /systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal | |
parent | 5322c05afdcd23cb0516e4e6e6c8739ed4616612 (diff) | |
download | org.eclipse.linuxtools-45e91c272ac6331915bcb12c00f6799062fa7847.tar.gz org.eclipse.linuxtools-45e91c272ac6331915bcb12c00f6799062fa7847.tar.xz org.eclipse.linuxtools-45e91c272ac6331915bcb12c00f6799062fa7847.zip |
systemtap: Removed unudse methods.
Change-Id: Idd3e5279af9cc2b209d268031c6769e82e9432e1
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
Reviewed-on: https://git.eclipse.org/r/24667
Tested-by: Hudson CI
Diffstat (limited to 'systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal')
11 files changed, 2 insertions, 3241 deletions
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/AbortFormatting.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/AbortFormatting.java deleted file mode 100644 index b11ffcffba..0000000000 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/AbortFormatting.java +++ /dev/null @@ -1,32 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2007, 2013 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - * Anton Leherbauer (Wind River Systems) - * Red Hat Inc. - modified for use in SystemTap - *******************************************************************************/ -package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp; - -/** - * Unchecked exception wrapping invalid input checked exception which may occur - * when scanning original formatted source. - * - * @since 4.0 - */ -public class AbortFormatting extends RuntimeException { - - private static final long serialVersionUID= -5796507276311428526L; - Throwable nestedException; - - public AbortFormatting(String message) { - super(message); - } - public AbortFormatting(Throwable cause) { - super(cause); - } -} diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/AlignmentException.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/AlignmentException.java deleted file mode 100644 index 0e3f39a11c..0000000000 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/AlignmentException.java +++ /dev/null @@ -1,53 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2008, 2013 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - * Red Hat Inc. - copied into SystemTap - *******************************************************************************/ -package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp; - -/** - * Exception used to backtrack and break available alignments - * When the exception is thrown, it is assumed that some alignment will be changed. - * - * @since 4.0 - */ -public class AlignmentException extends RuntimeException { - private static final long serialVersionUID= -1081237230006524966L; - public static final int LINE_TOO_LONG = 1; - public static final int ALIGN_TOO_SMALL = 2; - - int reason; - int value; - public int relativeDepth; - - public AlignmentException(int reason, int relativeDepth) { - this(reason, 0, relativeDepth); - } - - public AlignmentException(int reason, int value, int relativeDepth) { - this.reason = reason; - this.value = value; - this.relativeDepth = relativeDepth; - } - - @Override - public String toString() { - StringBuilder buffer = new StringBuilder(40); - switch (reason) { - case LINE_TOO_LONG: - buffer.append("LINE_TOO_LONG"); //$NON-NLS-1$ - break; - case ALIGN_TOO_SMALL: - buffer.append("ALIGN_TOO_SMALL"); //$NON-NLS-1$ - break; - } - buffer.append("<relativeDepth: ").append(relativeDepth).append(">\n"); //$NON-NLS-1$ //$NON-NLS-2$ - return buffer.toString(); - } -} diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/IndentUtil.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/IndentUtil.java index 5a4e93c38d..1d42ac1f3e 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/IndentUtil.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/IndentUtil.java @@ -20,7 +20,6 @@ import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITypedRegion; import org.eclipse.jface.text.TextUtilities; import org.eclipse.jface.text.source.ILineRange; -import org.eclipse.jface.text.source.LineRange; /** @@ -43,7 +42,6 @@ public final class IndentUtil { } private boolean[] commentLinesAtColumnZero; private boolean hasChanged; - private int leftmostLine= -1; /** * Returns <code>true</code> if the indentation operation changed the * document, <code>false</code> if not. @@ -96,22 +94,6 @@ public final class IndentUtil { } /** - * Inserts <code>indent</code> string at the beginning of each line in <code>lines</code>. - * @param document the document to be changed. - * @param lines the line range to be indented. - * @param indent the indent string to be inserted. - * @throws BadLocationException if <code>lines</code> is not a valid line - * range on <code>document</code> - */ - public static void indentLines(IDocument document, LineRange lines, String indent) throws BadLocationException { - int numberOfLines= lines.getNumberOfLines(); - for (int line= lines.getStartLine(), last= line + numberOfLines; line < last; line++) { - int offset= document.getLineOffset(line); - document.replace(offset, 0, indent); - } - } - - /** * Returns <code>true</code> if line comments at column 0 should be indented inside, <code>false</code> otherwise. * * @param project the project to get project specific options from @@ -135,217 +117,6 @@ public final class IndentUtil { } /** - * Shifts the line range specified by <code>lines</code> in - * <code>document</code>. The amount that the lines get shifted - * are determined by the first line in the range, all subsequent - * lines are adjusted accordingly. The passed C project may be - * <code>null</code>, it is used solely to obtain formatter - * preferences. - * - * @param document the document to be changed - * @param lines the line range to be shifted - * @param project the C project to get the formatter preferences - * from, or <code>null</code> if global preferences should - * be used - * @param result the result from a previous call to - * <code>shiftLines</code>, in order to maintain comment - * line properties, or <code>null</code>. Note that the - * passed result may be changed by the call. - * @return an indent result that may be queried for changes and can - * be reused in subsequent indentation operations - * @throws BadLocationException if <code>lines</code> is not a - * valid line range on <code>document</code> - */ - public static IndentResult shiftLines(IDocument document, ILineRange lines, IProject project, IndentResult result) throws BadLocationException { - int numberOfLines= lines.getNumberOfLines(); - - if (numberOfLines < 1) - return new IndentResult(null); - - result= reuseOrCreateToken(result, numberOfLines); - result.hasChanged= false; - - STPHeuristicScanner scanner= new STPHeuristicScanner(document); - STPIndenter indenter= new STPIndenter(document, scanner, project); - - boolean indentInsideLineComments= indentInsideLineComments(project); - String current= getCurrentIndent(document, lines.getStartLine(), indentInsideLineComments); - StringBuilder correct= new StringBuilder(computeIndent(document, lines.getStartLine(), indenter, scanner)); - - int tabSize= CodeFormatterUtil.getTabWidth(); - StringBuilder addition= new StringBuilder(); - int difference= subtractIndent(correct, current, addition, tabSize); - - if (difference == 0) - return result; - - if (result.leftmostLine == -1) - result.leftmostLine= getLeftMostLine(document, lines, tabSize, indentInsideLineComments); - - int maxReduction= computeVisualLength(getCurrentIndent(document, result.leftmostLine + lines.getStartLine(), indentInsideLineComments), tabSize); - - if (difference > 0) { - for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++) - addIndent(document, line, addition, result.commentLinesAtColumnZero, i++, indentInsideLineComments); - } else { - int reduction= Math.min(-difference, maxReduction); - for (int line= lines.getStartLine(), last= line + numberOfLines, i= 0; line < last; line++) - cutIndent(document, line, reduction, tabSize, result.commentLinesAtColumnZero, i++, indentInsideLineComments); - } - - result.hasChanged= true; - - return result; - - } - - /** - * Indents line <code>line</code> in <code>document</code> with <code>indent</code>. - * Leaves leading comment signs alone. - * - * @param document the document - * @param line the line - * @param indent the indentation to insert - * @param commentlines - * @param relative - * @param indentInsideLineComments option whether to indent inside line comments starting at column 0 - * @throws BadLocationException on concurrent document modification - */ - private static void addIndent(IDocument document, int line, CharSequence indent, boolean[] commentlines, int relative, boolean indentInsideLineComments) throws BadLocationException { - IRegion region= document.getLineInformation(line); - int insert= region.getOffset(); - int endOffset= region.getOffset() + region.getLength(); - - if (indentInsideLineComments) { - // go behind line comments - if (!commentlines[relative]) { - while (insert < endOffset - 2 && document.get(insert, 2).equals(SLASHES)) - insert += 2; - } - } - - // insert indent - document.replace(insert, 0, indent.toString()); - } - - /** - * Cuts the visual equivalent of <code>toDelete</code> characters out of the - * indentation of line <code>line</code> in <code>document</code>. - * - * @param document - * @param line - * @param shiftWidth - * @param tabWidth - * @return number of characters deleted - * @throws BadLocationException - */ - public static int cutIndent(IDocument document, int line, int shiftWidth, int tabWidth) throws BadLocationException { - return cutIndent(document, line, shiftWidth, tabWidth, new boolean[1], 0, false); - } - - /** - * Cuts the visual equivalent of <code>toDelete</code> characters out of the - * indentation of line <code>line</code> in <code>document</code>. Leaves - * leading comment signs alone if desired. - * - * @param document the document - * @param line the line - * @param toDelete the number of space equivalents to delete. - * @param commentLines - * @param relative - * @param indentInsideLineComments option whether to indent inside line comments starting at column 0 - * @return number of characters deleted - * @throws BadLocationException on concurrent document modification - */ - private static int cutIndent(IDocument document, int line, int toDelete, int tabSize, boolean[] commentLines, int relative, boolean indentInsideLineComments) throws BadLocationException { - IRegion region= document.getLineInformation(line); - int from= region.getOffset(); - int endOffset= region.getOffset() + region.getLength(); - - if (indentInsideLineComments) { - // go behind line comments - while (from < endOffset - 2 && document.get(from, 2).equals(SLASHES)) - from += 2; - } - - int to= from; - while (toDelete > 0 && to < endOffset) { - char ch= document.getChar(to); - if (!Character.isWhitespace(ch)) - break; - toDelete -= computeVisualLength(ch, tabSize); - if (toDelete >= 0) - to++; - else - break; - } - - if (endOffset > to + 1 && document.get(to, 2).equals(SLASHES)) - commentLines[relative]= true; - - document.replace(from, to - from, ""); //$NON-NLS-1$ - return to - from; - } - - /** - * Computes the difference of two indentations and returns the difference in - * length of current and correct. If the return value is positive, <code>addition</code> - * is initialized with a substring of that length of <code>correct</code>. - * - * @param correct the correct indentation - * @param current the current indentation (migth contain non-whitespace) - * @param difference a string buffer - if the return value is positive, it will be cleared and set to the substring of <code>current</code> of that length - * @return the difference in lenght of <code>correct</code> and <code>current</code> - */ - private static int subtractIndent(CharSequence correct, CharSequence current, StringBuilder difference, int tabSize) { - int c1= computeVisualLength(correct, tabSize); - int c2= computeVisualLength(current, tabSize); - int diff= c1 - c2; - if (diff <= 0) - return diff; - - difference.setLength(0); - int len= 0, i= 0; - while (len < diff) { - char c= correct.charAt(i++); - difference.append(c); - len += computeVisualLength(c, tabSize); - } - - - return diff; - } - - private static int computeVisualLength(char ch, int tabSize) { - if (ch == '\t') - return tabSize; - return 1; - } - - /** - * Returns the visual length of a given <code>CharSequence</code> taking into - * account the visual tabulator length. - * - * @param seq the string to measure - * @return the visual length of <code>seq</code> - */ - public static int computeVisualLength(CharSequence seq, int tablen) { - int size= 0; - - for (int i= 0; i < seq.length(); i++) { - char ch= seq.charAt(i); - if (ch == '\t') { - if (tablen != 0) - size += tablen - size % tablen; - // else: size stays the same - } else { - size++; - } - } - return size; - } - - /** * Returns the indentation of the line <code>line</code> in <code>document</code>. * The returned string may contain pairs of leading slashes that are considered * part of the indentation. @@ -378,21 +149,6 @@ public final class IndentUtil { return document.get(from, to - from); } - private static int getLeftMostLine(IDocument document, ILineRange lines, int tabSize, boolean indentInsideLineComments) throws BadLocationException { - int numberOfLines= lines.getNumberOfLines(); - int first= lines.getStartLine(); - int minLine= -1; - int minIndent= Integer.MAX_VALUE; - for (int line= 0; line < numberOfLines; line++) { - int length= computeVisualLength(getCurrentIndent(document, line + first, indentInsideLineComments), tabSize); - if (length < minIndent && document.getLineLength(line + first) > 0) { - minIndent= length; - minLine= line; - } - } - return minLine; - } - private static IndentResult reuseOrCreateToken(IndentResult token, int numberOfLines) { if (token == null) token= new IndentResult(new boolean[numberOfLines]); @@ -607,47 +363,4 @@ public final class IndentUtil { computed= new StringBuilder(previousIndent); return computed.toString(); } - - /** - * Extends the string with whitespace to match displayed width. - * @param prefix add to this string - * @param displayedWidth the desired display width - * @param tabWidth the configured tab width - * @param useSpaces whether to use spaces only - */ - public static String changePrefix(String prefix, int displayedWidth, int tabWidth, boolean useSpaces) { - int column = computeVisualLength(prefix, tabWidth); - if (column > displayedWidth) { - return prefix; - } - final StringBuilder buffer = new StringBuilder(prefix); - appendIndent(buffer, displayedWidth, tabWidth, useSpaces, column); - return buffer.toString(); - } - - /** - * Appends whitespace to given buffer such that its visual length equals the given width. - * @param buffer the buffer to add whitespace to - * @param width the desired visual indent width - * @param tabWidth the configured tab width - * @param useSpaces whether tabs should be substituted by spaces - * @param startColumn the column where to start measurement - * @return StringBuilder - */ - private static StringBuilder appendIndent(StringBuilder buffer, int width, int tabWidth, boolean useSpaces, int startColumn) { - assert tabWidth > 0; - int tabStop = startColumn - startColumn % tabWidth; - int tabs = useSpaces ? 0 : (width-tabStop) / tabWidth; - for (int i = 0; i < tabs; ++i) { - buffer.append('\t'); - tabStop += tabWidth; - startColumn = tabStop; - } - int spaces = width - startColumn; - for (int i = 0; i < spaces; ++i) { - buffer.append(' '); - } - return buffer; - } - } diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/OptimizedReplaceEdit.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/OptimizedReplaceEdit.java deleted file mode 100644 index d9459d1a9a..0000000000 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/OptimizedReplaceEdit.java +++ /dev/null @@ -1,30 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2011, 2013 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - * Sergey Prigogin (Google) - * Red Hat Inc. - modified for use in SystemTap - *******************************************************************************/ -package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp; - -public class OptimizedReplaceEdit { - int offset; - int length; - String replacement; - - public OptimizedReplaceEdit(int offset, int length, CharSequence replacement) { - this.offset = offset; - this.length = length; - this.replacement = replacement.toString(); - } - - @Override - public String toString() { - return "(" + this.offset + ", length " + this.length + " :>" + this.replacement + "<"; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ - } -} diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPAlignment.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPAlignment.java index a1b78beded..0859128b21 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPAlignment.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPAlignment.java @@ -11,7 +11,6 @@ *******************************************************************************/ package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp; -import java.util.Arrays; /** * Alignment management @@ -19,51 +18,6 @@ import java.util.Arrays; * @since 4.0 */ public class STPAlignment { - // Alignment names. - public static final String ASSIGNMENT_EXPRESSION = "assignmentExpression"; //$NON-NLS-1$ - public static final String BINARY_EXPRESSION = "binaryExpression"; //$NON-NLS-1$ - public static final String COMPACT_IF = "compactIf"; //$NON-NLS-1$ - public static final String CONDITIONAL_EXPRESSION = "conditionalExpression"; //$NON-NLS-1$ - public static final String CONDITIONAL_EXPRESSION_CHAIN = "conditionalExpressionChain"; //$NON-NLS-1$ - public static final String DECLARATION_INITIALIZER = "declarationInitializer"; //$NON-NLS-1$ - public static final String DESIGNATED_INITIALIZER = "designatedInitializer"; //$NON-NLS-1$ - public static final String EXCEPTION_SPECIFICATION = "exceptionSpecification"; //$NON-NLS-1$ - public static final String FIELD_REFERENCE = "fieldReference"; //$NON-NLS-1$ - public static final String FOR = "for"; //$NON-NLS-1$ - public static final String LIST_ELEMENTS_PREFIX = "listElements_"; //$NON-NLS-1$ - public static final String MACRO_ARGUMENTS = "macroArguments"; //$NON-NLS-1$ - public static final String OVERLOADED_LEFT_SHIFT_CHAIN = "overloadedLeftShiftChain"; //$NON-NLS-1$ - public static final String TRAILING_TEXT = "trailingText"; //$NON-NLS-1$ - - /** The name of the alignment */ - public String name; - - /** Link to the enclosing alignment. */ - public STPAlignment enclosing; - - /** Start location of this alignment. */ - public STPLocation location; - - /** - * Tail formatter is an encapsulation mechanism for formatting of the trailing text that should - * be kept together with the last element of a list. - */ - public Runnable tailFormatter; - - // Indentation management - private int fragmentIndex; - private int fragmentCount; - private int[] fragmentIndentations; - - // Break management - public int breakIndentationLevel; - private int alternativeBreakIndentationLevel; - public int[] fragmentBreaks; - public boolean wasSplit; - private int currentFragmentStartLine; - - private final STPScribe scribe; - /* * Alignment modes */ @@ -113,278 +67,7 @@ public class STPAlignment { */ public static final int M_NEXT_PER_LINE_SPLIT = 64 + 16; // one per line, except first fragment (if possible) - //64+32 - //64+32+16 - - // Mode controlling column alignments - /** - * <table BORDER COLS=4 WIDTH="100%" > - * <tr><td>#fragment1A</td> <td>#fragment2A</td> <td>#fragment3A</td> <td>#very-long-fragment4A</td></tr> - * <tr><td>#fragment1B</td> <td>#long-fragment2B</td> <td>#fragment3B</td> <td>#fragment4B</td></tr> - * <tr><td>#very-long-fragment1C</td> <td>#fragment2C</td> <td>#fragment3C</td> <td>#fragment4C</td></tr> - * </table> - */ - public static final int M_MULTICOLUMN = 256; // fragments are on same line, but multiple line of fragments will be aligned vertically - public static final int M_NO_ALIGNMENT = 0; - private int mode; - public static final int SPLIT_MASK = M_ONE_PER_LINE_SPLIT | M_NEXT_SHIFTED_SPLIT | M_COMPACT_SPLIT | M_COMPACT_FIRST_BREAK_SPLIT | M_NEXT_PER_LINE_SPLIT; - - // Alignment tie-break rules - when split is needed, will decide whether innermost/outermost alignment is to be chosen - public static final int R_OUTERMOST = 1; - public static final int R_INNERMOST = 2; - public int tieBreakRule; - - // Alignment effects on a per fragment basis - public static final int BREAK_NOT_ALLOWED = -1; - public static final int NONE = 0; - public static final int BREAK = 2; - - // Location to align and break on. - public STPAlignment(String name, int mode, int tieBreakRule, STPScribe scribe, int fragmentCount, - int sourceRestart, int continuationIndent) { - this.name = name; - this.location = new STPLocation(scribe, sourceRestart); - this.mode = mode; - this.tieBreakRule = tieBreakRule; - this.fragmentCount = fragmentCount; - this.scribe = scribe; - this.wasSplit = false; - this.fragmentIndentations = new int[this.fragmentCount]; - this.fragmentBreaks = new int[this.fragmentCount]; - Arrays.fill(this.fragmentBreaks, (this.mode & M_FORCE) == 0 ? BREAK_NOT_ALLOWED : NONE); - this.currentFragmentStartLine = this.scribe.line; - - int currentColumn = this.location.outputColumn; - if (currentColumn == 1) { - currentColumn = this.location.outputIndentationLevel + 1; - } - - // Initialize the break indentation level, using modes and continuationIndentationLevel - // preference. - final int indentSize = this.scribe.indentationSize; - - if ((mode & M_INDENT_ON_COLUMN) != 0) { - // Indent broken fragments at next indentation level, based on current column - this.breakIndentationLevel = this.scribe.getNextIndentationLevel(currentColumn); - this.alternativeBreakIndentationLevel = - this.location.outputIndentationLevel + continuationIndent * indentSize; - } else { - int baseIndentationLevel = this.location.outputIndentationLevel; - if (name != TRAILING_TEXT && this.scribe.currentAlignment != null && - (this.scribe.currentAlignment.mode & M_INDENT_ON_COLUMN) != 0 && - this.scribe.currentAlignment.fragmentCount > 1) { - baseIndentationLevel = this.scribe.currentAlignment.breakIndentationLevel; - } - if ((mode & M_INDENT_BY_ONE) != 0) { - // Indent broken fragments exactly one level deeper than current indentation - this.breakIndentationLevel = baseIndentationLevel + indentSize; - } else { - this.breakIndentationLevel = baseIndentationLevel + continuationIndent * indentSize; - } - this.alternativeBreakIndentationLevel = this.breakIndentationLevel; - } - - // Check for forced alignments - if ((this.mode & M_FORCE) != 0) { - this.fragmentBreaks[this.fragmentIndex] = NONE; - couldBreak(); - } - } - - public boolean couldBreak() { - int i; - switch (mode & SPLIT_MASK) { - /* # aligned fragment - * foo( - * #AAAAA, #BBBBB, - * #CCCC); - */ - case M_COMPACT_FIRST_BREAK_SPLIT: - if (this.fragmentBreaks[0] == NONE) { - if ((this.mode & M_INDENT_ON_COLUMN) != 0) { - if (this.breakIndentationLevel <= this.alternativeBreakIndentationLevel) { - // Does not make sense to break here unless indentation is reduced. - break; - } - // Change break indentation level and erase previously created breaks. - this.breakIndentationLevel = this.alternativeBreakIndentationLevel; - eraseExistingBreaks(0); - } - this.fragmentBreaks[0] = BREAK; - this.fragmentIndentations[0] = this.breakIndentationLevel; - return wasSplit = true; - } - i = this.fragmentIndex; - do { - if (this.fragmentBreaks[i] == NONE) { - this.fragmentBreaks[i] = BREAK; - this.fragmentIndentations[i] = this.breakIndentationLevel; - return wasSplit = true; - } - } while (--i >= 0); - break; - /* # aligned fragment - * foo(#AAAAA, #BBBBB, - * #CCCC); - */ - case M_COMPACT_SPLIT: - i = this.fragmentIndex; - do { - if (this.fragmentBreaks[i] == NONE) { - if ((this.mode & M_INDENT_ON_COLUMN) != 0 && isFirstBreakableFragment(i)) { - if (this.breakIndentationLevel <= this.alternativeBreakIndentationLevel) { - // Does not make sense to break here unless indentation is reduced. - break; - } - // Change break indentation level and erase previously created breaks. - this.breakIndentationLevel = this.alternativeBreakIndentationLevel; - eraseExistingBreaks(i); - } - this.fragmentBreaks[i] = BREAK; - this.fragmentIndentations[i] = this.breakIndentationLevel; - return wasSplit = true; - } - } while ((this.fragmentBreaks[i] != BREAK || (this.mode & M_INDENT_ON_COLUMN) != 0) && --i >= 0); - break; - - /* # aligned fragment - * foo( - * #AAAAA, - * #BBBBB, - * #CCCC); - */ - case M_NEXT_SHIFTED_SPLIT: - if (this.fragmentBreaks[0] == NONE) { - this.fragmentBreaks[0] = BREAK; - this.fragmentIndentations[0] = this.breakIndentationLevel; - for (i = 1; i < this.fragmentCount; i++) { - this.fragmentBreaks[i] = BREAK; - this.fragmentIndentations[i] = - this.breakIndentationLevel + this.scribe.indentationSize; - } - return wasSplit = true; - } - break; - - /* # aligned fragment - * foo( - * #AAAAA, - * #BBBBB, - * #CCCC); - */ - case M_ONE_PER_LINE_SPLIT: - if (this.fragmentBreaks[0] == NONE) { - for (i = 0; i < this.fragmentCount; i++) { - this.fragmentBreaks[i] = BREAK; - this.fragmentIndentations[i] = this.breakIndentationLevel; - } - return wasSplit = true; - } - break; - /* # aligned fragment - * foo(#AAAAA, - * #BBBBB, - * #CCCC); - */ - case M_NEXT_PER_LINE_SPLIT: - if (this.fragmentBreaks[0] != BREAK) { - if (this.fragmentCount > 1 && this.fragmentBreaks[1] == NONE) { - if ((this.mode & M_INDENT_ON_COLUMN) != 0) { - this.fragmentIndentations[0] = this.breakIndentationLevel; - } - for (i = 1; i < this.fragmentCount; i++) { - this.fragmentBreaks[i] = BREAK; - this.fragmentIndentations[i] = this.breakIndentationLevel; - } - return wasSplit = true; - } - } - break; - } - return false; // Cannot split better - } - - private boolean isFirstBreakableFragment(int i) { - while (--i >= 0) { - if (this.fragmentBreaks[i] != BREAK_NOT_ALLOWED) - return false; - } - return true; - } - - private void eraseExistingBreaks(int startFragmentIndex) { - for (int j = startFragmentIndex + 1; j < this.fragmentIndentations.length; j++) { - if (this.fragmentBreaks[j] == BREAK) { - this.fragmentBreaks[j] = NONE; - this.fragmentIndentations[j] = 0; - } - } - } - - // Performs alignment effect for current fragment. - public void performFragmentEffect() { - if ((this.mode & M_MULTICOLUMN) == 0) { - switch (this.mode & SPLIT_MASK) { - case STPAlignment.M_COMPACT_SPLIT: - case STPAlignment.M_COMPACT_FIRST_BREAK_SPLIT: - case STPAlignment.M_NEXT_PER_LINE_SPLIT: - case STPAlignment.M_NEXT_SHIFTED_SPLIT: - case STPAlignment.M_ONE_PER_LINE_SPLIT: - break; - default: - return; - } - } - - if ((this.mode & M_INDENT_ON_COLUMN) != 0 && this.fragmentIndex > 0 && - this.scribe.line > currentFragmentStartLine) { - // The previous fragment spans multiple line. Put the current fragment on a new line. - this.fragmentBreaks[this.fragmentIndex] = BREAK; - this.fragmentIndentations[this.fragmentIndex] = this.breakIndentationLevel; - wasSplit = true; - } - if (this.fragmentBreaks[this.fragmentIndex] == BREAK) { - this.scribe.startNewLine(); - } - if (this.fragmentIndentations[this.fragmentIndex] > 0) { - this.scribe.indentationLevel = this.fragmentIndentations[this.fragmentIndex]; - } - currentFragmentStartLine = this.scribe.line; - } - - @Override - public String toString() { - StringBuffer buffer = new StringBuffer(10); - buffer - .append(getClass().getName()) - .append(':') - .append("<name: ") //$NON-NLS-1$ - .append(this.name) - .append(">"); //$NON-NLS-1$ - if (this.enclosing != null) { - buffer - .append("<enclosingName: ") //$NON-NLS-1$ - .append(this.enclosing.name) - .append('>'); - } - buffer.append('\n'); - - for (int i = 0; i < this.fragmentCount; i++) { - buffer - .append(" - fragment ") //$NON-NLS-1$ - .append(i) - .append(": ") //$NON-NLS-1$ - .append("<break: ") //$NON-NLS-1$ - .append(this.fragmentBreaks[i] > 0 ? "YES" : "NO") //$NON-NLS-1$ //$NON-NLS-2$ - .append(">") //$NON-NLS-1$ - .append("<indent: ") //$NON-NLS-1$ - .append(this.fragmentIndentations[i]) - .append(">\n"); //$NON-NLS-1$ - } - buffer.append('\n'); - return buffer.toString(); - } -} +}
\ No newline at end of file diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPIndenter.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPIndenter.java index cdac5eef65..674133c682 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPIndenter.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPIndenter.java @@ -504,25 +504,6 @@ public final class STPIndenter { } /** - * Computes the indentation for a continuation line at <code>offset</code>. - * - * @param offset the offset in the document - * @return a StringBuilder which reflects the correct indentation for - * the line in which offset resides, or <code>null</code> if it cannot be - * determined. - * @throws BadLocationException - */ - public StringBuilder computeContinuationLineIndentation(int offset) throws BadLocationException { - StringBuilder reference= getLeadingWhitespace(offset); - IRegion line= fDocument.getLineInformationOfOffset(offset); - String string= fDocument.get(line.getOffset(), offset - line.getOffset()); - if (string.trim().isEmpty()) - return reference; - // Add additional indent - return createReusingIndent(reference, fPrefs.prefContinuationIndent, 0); - } - - /** * Computes the length of a <code>CharacterSequence</code>, counting * a tab character as the size until the next tab stop and every other * character as one. diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPLocation.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPLocation.java deleted file mode 100644 index 3e2a45bb05..0000000000 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPLocation.java +++ /dev/null @@ -1,53 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2006, 2013 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - * Anton Leherbauer (Wind River Systems) - * Red Hat Inc. - modified for use in SystemTap - *******************************************************************************/ -package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp; - -/** - * A location maintains positional information both in original source - * and in the output source. - * It remembers source offsets, line/column and indentation level. - * - * @since 4.0 - */ -public class STPLocation { - public int inputOffset; - public int outputLine; - public int outputColumn; - public int outputIndentationLevel; - public boolean needSpace; - public boolean pendingSpace; - public int numberOfIndentations; - - // chunk management - public int lastNumberOfNewLines; - - // edits management - int editsIndex; - OptimizedReplaceEdit textEdit; - - public Runnable tailFormatter; - - public STPLocation(STPScribe scribe, int sourceRestart){ - this.inputOffset = sourceRestart; - this.outputLine = scribe.line; - this.outputColumn = scribe.column; - this.outputIndentationLevel = scribe.indentationLevel; - this.needSpace = scribe.needSpace; - this.pendingSpace = scribe.pendingSpace; - this.numberOfIndentations = scribe.numberOfIndentations; - this.lastNumberOfNewLines = scribe.lastNumberOfNewLines; - this.editsIndex = scribe.editsIndex; - this.textEdit = scribe.getLastEdit(); - this.tailFormatter = scribe.getTailFormatter(); - } -} diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPScribe.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPScribe.java deleted file mode 100644 index 3ac1ca89cd..0000000000 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/STPScribe.java +++ /dev/null @@ -1,1481 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2011, 2013 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - * Anton Leherbauer (Wind River Systems) - * Sergey Prigogin (Google) - * Red Hat Inc. - modified for use in SystemTap - *******************************************************************************/ -package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp; - -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -import org.eclipse.jface.text.Position; - -/** - * This class is responsible for dumping formatted source. - * - * @since 4.0 - */ -public class STPScribe { - private static final String EMPTY_STRING= ""; //$NON-NLS-1$ - private static final String SPACE= " "; //$NON-NLS-1$ - private static final char[] EMPTY_CHAR_ARRAY = {}; - - private final STPDefaultCodeFormatterOptions preferences; - public final Scanner scanner; - - /** one-based column */ - public int column= 1; - - // Most specific alignment. - public STPAlignment currentAlignment; - public STPAlignment memberAlignment; - - /** @see Alignment#tailFormatter */ - private Runnable tailFormatter; - - private Token currentToken; - - // Edits management - private OptimizedReplaceEdit[] edits; - public int editsIndex; - - public int indentationLevel; - public int numberOfIndentations; - public int indentationSize; - - private final String lineSeparator; - private final boolean indentEmptyLines; - private final int pageWidth; - private boolean preserveNewLines; - private boolean checkLineWrapping; - public int lastNumberOfNewLines; - private boolean preserveLineBreakIndentation; - private boolean formatBrace; - public int line; - - public boolean needSpace; - public boolean pendingSpace; - - private int tabLength; - private int tabChar; - private final boolean useTabsOnlyForLeadingIndents; - - private int scannerEndPosition; - - private List<Position> fSkipPositions= Collections.emptyList(); - - private boolean skipOverInactive; - - private int fSkipStartOffset= Integer.MAX_VALUE; - private int fSkipEndOffset; - - /* Comments formatting */ - private static final int NO_TRAILING_COMMENT = 0x0000; - private static final int BASIC_TRAILING_COMMENT = 0x0100; - private int[] lineOffsets; - private int numLines; - - // Class to store previous line comment information - private static class LineComment { - boolean contiguous; - int currentIndentation; - int indentation; - char[] leadingSpaces = EMPTY_CHAR_ARRAY; - } - final LineComment lastLineComment = new LineComment(); - - STPScribe() { - scanner= new Scanner(); - preferences= new STPDefaultCodeFormatterOptions(null); - pageWidth= preferences.page_width; - tabLength= preferences.tab_size; - indentationLevel= 0; // initialize properly - numberOfIndentations= 0; - useTabsOnlyForLeadingIndents= preferences.use_tabs_only_for_leading_indentations; - indentEmptyLines= preferences.indent_empty_lines; - tabChar= preferences.tab_char; - if (tabChar == STPDefaultCodeFormatterOptions.MIXED) { - indentationSize= preferences.indentation_size; - } else { - indentationSize= tabLength; - } - lineSeparator= preferences.line_separator; - indentationLevel= preferences.initial_indentation_level * indentationSize; - preserveNewLines = false; - reset(); - } - - private final void addDeleteEdit(int start, int end) { - addOptimizedReplaceEdit(start, end - start + 1, EMPTY_STRING); - } - - private final void addInsertEdit(int insertPosition, CharSequence insertedString) { - addOptimizedReplaceEdit(insertPosition, 0, insertedString); - } - - /** - * Adds a replace edit. - * @param start start offset (inclusive) - * @param end end offset (inclusive) - * @param replacement the replacement string - */ - private final void addReplaceEdit(int start, int end, CharSequence replacement) { - addOptimizedReplaceEdit(start, end - start + 1, replacement); - } - - private final void addOptimizedReplaceEdit(int offset, int length, CharSequence replacement) { - if (edits.length == editsIndex) { - resize(); - } - if (editsIndex > 0) { - // Try to merge last two edits - final OptimizedReplaceEdit previous= edits[editsIndex - 1]; - final int previousOffset= previous.offset; - final int previousLength= previous.length; - final int endOffsetOfPreviousEdit= previousOffset + previousLength; - final int replacementLength= replacement.length(); - final String previousReplacement= previous.replacement; - final int previousReplacementLength= previousReplacement.length(); - if (previousOffset == offset && previousLength == length - && (replacementLength == 0 || previousReplacementLength == 0)) { - if (currentAlignment != null) { - final STPLocation location= currentAlignment.location; - if (location.editsIndex == editsIndex) { - location.editsIndex--; - location.textEdit= previous; - } - } - editsIndex--; - return; - } - if (endOffsetOfPreviousEdit == offset) { - if (length != 0) { - if (replacementLength != 0) { - editsIndex--; - appendOptimizedReplaceEdit(previousOffset, previousLength + length, - previousReplacement + replacement); - } else if (previousLength + length == previousReplacementLength) { - // Check the characters. If they are identical, - // we can get rid of the previous edit. - boolean canBeRemoved= true; - loop: for (int i= previousOffset; i < previousOffset + previousReplacementLength; i++) { - if (scanner.source[i] != previousReplacement.charAt(i - previousOffset)) { - editsIndex--; - appendOptimizedReplaceEdit(previousOffset, previousReplacementLength, - previousReplacement); - canBeRemoved= false; - break loop; - } - } - if (canBeRemoved) { - if (currentAlignment != null) { - final STPLocation location= currentAlignment.location; - if (location.editsIndex == editsIndex) { - location.editsIndex--; - location.textEdit= previous; - } - } - editsIndex--; - } - } else { - editsIndex--; - appendOptimizedReplaceEdit(previousOffset, previousLength + length, - previousReplacement); - } - } else { - if (replacementLength != 0) { - editsIndex--; - appendOptimizedReplaceEdit(previousOffset, previousLength, - previousReplacement + replacement); - } - } - } else { - assert endOffsetOfPreviousEdit < offset; - appendOptimizedReplaceEdit(offset, length, replacement); - } - } else { - appendOptimizedReplaceEdit(offset, length, replacement); - } - } - - /** - * Trims redundant prefix from a replacement edit and, if there is anything left, appends - * the replacement edit to the edits array. - */ - private void appendOptimizedReplaceEdit(int offset, int length, CharSequence replacement) { - int replacementLength = replacement.length(); - int i; - for (i = 0; i < length && i < replacementLength; i++, offset++) { - if (scanner.source[offset] != replacement.charAt(i)) - break; - } - length -= i; - if (i > 0) { - replacement = i == replacementLength ? - EMPTY_STRING : replacement.subSequence(i, replacementLength); - } - if (length > 0 || replacement.length() > 0) { - edits[editsIndex++]= new OptimizedReplaceEdit(offset, length, replacement); - } - } - - private int getIndentationOfOffset(int offset) { - int beginningOfLine = getLineStart(offset); - int indent = 0; - for (int i= beginningOfLine; i < offset; i++) { - indent = computeIndentation(scanner.source[i], indent); - } - return indent; - } - - /** - * Computes indentation after applying a character at a given indentation position. - * @param text the text to be applied. - * @param indent the initial indentation. - * @return the resulting indentation. - */ - private int computeIndentation(char c, int indent) { - switch (c) { - case '\t': - return tabLength > 0 ? indent + tabLength - indent % tabLength : indent; - case '\r': - case '\n': - return 0; - default: - return indent + 1; - } - } - - /** - * Computes indentation after applying a given text at a given indentation position. - * @param text the text to be applied. - * @param indent the initial indentation. - * @return the resulting indentation. - */ - private int computeIndentation(char[] text, int indent) { - if (text == null) - return indent; - int length = text.length; - for (int i = 0; i < length; i++) { - indent = computeIndentation(text[i], indent); - } - return indent; - } - - private int computeIndentation(CharSequence text, int indent) { - if (text == null) - return indent; - int length = text.length(); - for (int i = 0; i < length; i++) { - indent = computeIndentation(text.charAt(i), indent); - } - return indent; - } - - private String getEmptyLines(int linesNumber) { - StringBuilder buffer= new StringBuilder(); - if (lastNumberOfNewLines == 0) { - linesNumber++; // add an extra line breaks - for (int i= 0; i < linesNumber; i++) { - if (indentEmptyLines) - printIndentationIfNecessary(buffer); - buffer.append(lineSeparator); - } - lastNumberOfNewLines += linesNumber; - line += linesNumber; - column= 1; - needSpace= false; - pendingSpace= false; - } else if (lastNumberOfNewLines == 1) { - for (int i= 0; i < linesNumber; i++) { - if (indentEmptyLines) - printIndentationIfNecessary(buffer); - buffer.append(lineSeparator); - } - lastNumberOfNewLines += linesNumber; - line += linesNumber; - column= 1; - needSpace= false; - pendingSpace= false; - } else { - if ((lastNumberOfNewLines - 1) >= linesNumber) { - // there is no need to add new lines - return EMPTY_STRING; - } - final int realNewLineNumber= linesNumber - lastNumberOfNewLines + 1; - for (int i= 0; i < realNewLineNumber; i++) { - if (indentEmptyLines) - printIndentationIfNecessary(buffer); - buffer.append(lineSeparator); - } - lastNumberOfNewLines += realNewLineNumber; - line += realNewLineNumber; - column= 1; - needSpace= false; - pendingSpace= false; - } - return String.valueOf(buffer); - } - - public OptimizedReplaceEdit getLastEdit() { - if (editsIndex > 0) { - return edits[editsIndex - 1]; - } - return null; - } - - private String getNewLine() { - if (lastNumberOfNewLines >= 1) { - column= 1; // Ensure that the scribe is at the beginning of a new line - return EMPTY_STRING; - } - line++; - lastNumberOfNewLines= 1; - column= 1; - needSpace= false; - pendingSpace= false; - return lineSeparator; - } - - /** - * Returns next indentation level based on column estimated position (if - * column is not indented, then uses indentationLevel) - */ - public int getNextIndentationLevel(int someColumn) { - int indent= someColumn - 1; - if (indent == 0) - return indentationLevel; - if (tabChar == STPDefaultCodeFormatterOptions.TAB && !useTabsOnlyForLeadingIndents) { - // Round up to a multiple of indentationSize. - indent += indentationSize - 1; - return indent - indent % indentationSize; - } else { - return indent; - } - } - - private String getPreserveEmptyLines(int count) { - if (count == 0 && !preserveNewLines) { - // preserve line breaks in wrapping if specified - if (!preferences.join_wrapped_lines && lastNumberOfNewLines == 0) { - // Create new line - StringBuilder tempBuffer = new StringBuilder(); - tempBuffer.append(getNewLine()); - - if (currentAlignment != null && !formatBrace) { - indentationLevel = currentAlignment.breakIndentationLevel; - } - - // Set the flag to indicate that a specific indentation is currently in used - preserveLineBreakIndentation = true; - - // Print the computed indentation in the buffer - printIndentationIfNecessary(tempBuffer); - - return tempBuffer.toString(); - } - return EMPTY_STRING; - } - if (preferences.number_of_empty_lines_to_preserve != 0) { - int linesToPreserve= Math.min(count, preferences.number_of_empty_lines_to_preserve); - return getEmptyLines(linesToPreserve); - } else if (preserveNewLines) { - return getNewLine(); - } - return EMPTY_STRING; - } - - private void handleLineTooLong() { - // Search for closest breakable alignment, using tie-breaking rules. - // Look for innermost breakable one. - int relativeDepth= 0; - STPAlignment targetAlignment= currentAlignment; - while (targetAlignment != null && targetAlignment.tieBreakRule == STPAlignment.R_INNERMOST) { - if (targetAlignment.couldBreak()) { - throwAlignmentException(AlignmentException.LINE_TOO_LONG, relativeDepth); - } - targetAlignment= targetAlignment.enclosing; - relativeDepth++; - } - - // Look for outermost breakable one. - relativeDepth= 0; - int outerMostDepth= -1; - targetAlignment= currentAlignment; - while (targetAlignment != null) { - if (targetAlignment.tieBreakRule == STPAlignment.R_OUTERMOST && targetAlignment.couldBreak()) { - outerMostDepth= relativeDepth; - } - targetAlignment= targetAlignment.enclosing; - relativeDepth++; - } - if (outerMostDepth >= 0) { - throwAlignmentException(AlignmentException.LINE_TOO_LONG, outerMostDepth); - } - - // Look for innermost breakable one but don't stop if we encounter a R_OUTERMOST - // tie-breaking rule. - relativeDepth= 0; - targetAlignment= currentAlignment; - while (targetAlignment != null) { - if (targetAlignment.couldBreak()) { - throwAlignmentException(AlignmentException.LINE_TOO_LONG, relativeDepth); - } - targetAlignment= targetAlignment.enclosing; - relativeDepth++; - } - // Did not find any breakable location - proceed - } - - private void throwAlignmentException(int kind, int relativeDepth) { - AlignmentException e= new AlignmentException(kind, relativeDepth); - throw e; - } - - private void indent() { - if (shouldSkip(scanner.getCurrentPosition())) { - return; - } - indentationLevel += indentationSize; - numberOfIndentations++; - } - - /** - * Returns offset of the start of the line containing a given offset. - */ - private int getLineStart(int offset) { - // Binary search - int down = 0; - int up = numLines; - while (true) { - int mid = (down + up) / 2; - int lineOffset = lineOffsets[mid]; - if (mid == down) { - return lineOffset; - } - if (lineOffset > offset) { - up = mid; - } else { - down = mid; - } - } - } - - private boolean isOnFirstColumn(int offset) { - return getLineStart(offset) == offset; - } - - private void preserveEmptyLines(int count, int insertPosition) { - if (count > 0) { - if (preferences.number_of_empty_lines_to_preserve != 0) { - int linesToPreserve= Math.min(count, preferences.number_of_empty_lines_to_preserve); - printEmptyLines(linesToPreserve, insertPosition); - } else { - printNewLine(insertPosition); - } - } else { - printNewLine(insertPosition); - } - } - - private void printRaw(int startOffset, int length) { - if (length <= 0) { - return; - } - int currentPosition= scanner.getCurrentPosition(); - if (shouldSkip(currentPosition)) { - return; - } - if (startOffset > currentPosition) { - printComment(); - currentPosition= scanner.getCurrentPosition(); - } - if (startOffset + length < currentPosition) { - return; // Don't move backwards - } - boolean savedPreserveNL= preserveNewLines; - boolean savedSkipOverInactive= skipOverInactive; - int savedScannerEndPos= scannerEndPosition; - preserveNewLines= true; - skipOverInactive= false; - scannerEndPosition= startOffset + length; - try { - scanner.resetTo(Math.max(startOffset, currentPosition), startOffset + length); - int parenLevel= 0; - while (true) { - boolean hasWhitespace= printComment(); - currentToken= scanner.nextToken(); - if (currentToken == null) { - if (hasWhitespace) { - space(); - } - break; - } - if (pendingSpace) { - addInsertEdit(scanner.getCurrentTokenStartPosition(), SPACE); - pendingSpace= false; - needSpace= false; - } - switch (currentToken.type) { - case Token.tLBRACE: { - scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition); - formatOpeningBrace(preferences.brace_position_for_block, - preferences.insert_space_before_opening_brace_in_block); - if (preferences.indent_statements_compare_to_block) { - indent(); - } - break; - } - case Token.tRBRACE: { - scanner.resetTo(scanner.getCurrentTokenStartPosition(), scannerEndPosition); - if (preferences.indent_statements_compare_to_block) { - unIndent(); - } - formatClosingBrace(preferences.brace_position_for_block); - break; - } - case Token.tLPAREN: - ++parenLevel; - print(currentToken.getLength(), hasWhitespace); - if (parenLevel > 0) { - indentForContinuation(); - if (column <= indentationLevel) { - // HACK: avoid indent in same line - column= indentationLevel + 1; - } - } - break; - case Token.tRPAREN: - --parenLevel; - if (parenLevel >= 0) { - unIndentForContinuation(); - } - print(currentToken.getLength(), hasWhitespace); - break; - case Token.tSEMI: - print(currentToken.getLength(), preferences.insert_space_before_semicolon); - break; - case Token.t_catch: - case Token.t_else: - if (preferences.insert_new_line_before_else_in_if_statement) { - printNewLine(currentToken.offset); - } else { - hasWhitespace= true; - } - print(currentToken.getLength(), hasWhitespace); - break; - default: - if (currentToken.isVisibilityModifier() - && !preferences.indent_access_specifier_compare_to_type_header) { - int indentLevel= indentationLevel; - if (indentationLevel > 0) - unIndent(); - print(currentToken.getLength(), hasWhitespace); - while (indentationLevel < indentLevel) { - indent(); - } - } else { - print(currentToken.getLength(), hasWhitespace); - } - } - hasWhitespace= false; - } - } finally { - scannerEndPosition= savedScannerEndPos; - scanner.resetTo(startOffset + length, scannerEndPosition); - skipOverInactive= savedSkipOverInactive; - preserveNewLines= savedPreserveNL; - } - } - - private void indentForContinuation() { - for (int i= 0; i < preferences.continuation_indentation; i++) { - indent(); - } - } - - private void unIndentForContinuation() { - for (int i= 0; i < preferences.continuation_indentation; i++) { - unIndent(); - } - } - - private void formatOpeningBrace(String bracePosition, boolean insertSpaceBeforeBrace) { - if (STPDefaultCodeFormatterConstants.NEXT_LINE.equals(bracePosition)) { - printNewLine(); - } else if (STPDefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(bracePosition)) { - printNewLine(); - indent(); - } - printNextToken(Token.tLBRACE, insertSpaceBeforeBrace); - - printTrailingComment(); - } - - private void formatClosingBrace(String block_brace_position) { - printNextToken(Token.tRBRACE); - printTrailingComment(); - if (STPDefaultCodeFormatterConstants.NEXT_LINE_SHIFTED.equals(block_brace_position)) { - unIndent(); - } - } - - private void print(int length, boolean considerSpaceIfAny) { - if (checkLineWrapping && length + column - 1 > pageWidth) { - handleLineTooLong(); - } - lastNumberOfNewLines= 0; - if (indentationLevel != 0) { - printIndentationIfNecessary(); - } - if (considerSpaceIfAny) { - space(); - } - if (pendingSpace) { - addInsertEdit(scanner.getCurrentTokenStartPosition(), SPACE); - } - if (checkLineWrapping && length + column - 1 > pageWidth) { - handleLineTooLong(); - } - pendingSpace= false; - column += length; - needSpace= true; - } - - private void printBlockComment(boolean forceNewLine) { - int currentTokenStartPosition= scanner.getCurrentTokenStartPosition(); - int currentTokenEndPosition= scanner.getCurrentTokenEndPosition() + 1; - - scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition); - int currentCharacter; - boolean isNewLine= false; - int start= currentTokenStartPosition; - int nextCharacterStart= currentTokenStartPosition; - printIndentationIfNecessary(); - if (pendingSpace) { - addInsertEdit(currentTokenStartPosition, SPACE); - } - needSpace= false; - pendingSpace= false; - int previousStart= currentTokenStartPosition; - - while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter= scanner.getNextChar()) != -1) { - nextCharacterStart= scanner.getCurrentPosition(); - - switch (currentCharacter) { - case '\r': - if (isNewLine) { - line++; - } - start= previousStart; - isNewLine= true; - if (scanner.getNextChar('\n')) { - currentCharacter= '\n'; - nextCharacterStart= scanner.getCurrentPosition(); - } - break; - case '\n': - if (isNewLine) { - line++; - } - start= previousStart; - isNewLine= true; - break; - default: - if (isNewLine) { - if (Character.isWhitespace((char) currentCharacter)) { - int previousStartPosition= scanner.getCurrentPosition(); - while (currentCharacter != -1 && currentCharacter != '\r' && - currentCharacter != '\n' && - Character.isWhitespace((char) currentCharacter)) { - previousStart= nextCharacterStart; - previousStartPosition= scanner.getCurrentPosition(); - currentCharacter= scanner.getNextChar(); - nextCharacterStart= scanner.getCurrentPosition(); - } - if (currentCharacter == '\r' || currentCharacter == '\n') { - nextCharacterStart= previousStartPosition; - } - } - column= 1; - line++; - - StringBuilder buffer= new StringBuilder(); - buffer.append(lineSeparator); - printIndentationIfNecessary(buffer); - buffer.append(' '); - - addReplaceEdit(start, previousStart - 1, String.valueOf(buffer)); - } else { - column += (nextCharacterStart - previousStart); - } - isNewLine= false; - } - previousStart= nextCharacterStart; - scanner.setCurrentPosition(nextCharacterStart); - } - lastNumberOfNewLines= 0; - needSpace= false; - scanner.resetTo(currentTokenEndPosition, scannerEndPosition); - if (forceNewLine) { - startNewLine(); - } - } - - private void printPreprocessorDirective() { - int currentTokenStartPosition= scanner.getCurrentTokenStartPosition(); - int currentTokenEndPosition= scanner.getCurrentTokenEndPosition() + 1; - - scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition); - int currentCharacter; - boolean isNewLine= false; - int nextCharacterStart= currentTokenStartPosition; - needSpace= false; - pendingSpace= false; - int previousStart= currentTokenStartPosition; - - while (nextCharacterStart <= currentTokenEndPosition && - (currentCharacter= scanner.getNextChar()) != -1) { - nextCharacterStart= scanner.getCurrentPosition(); - - switch (currentCharacter) { - case '\r': - isNewLine= true; - if (scanner.getNextChar('\n')) { - currentCharacter= '\n'; - nextCharacterStart= scanner.getCurrentPosition(); - } - break; - case '\n': - isNewLine= true; - break; - default: - if (isNewLine) { - column= 1; - line++; - } else { - column += (nextCharacterStart - previousStart); - } - isNewLine= false; - } - previousStart= nextCharacterStart; - scanner.setCurrentPosition(nextCharacterStart); - } - lastNumberOfNewLines= isNewLine ? 1 : 0; - needSpace= false; - if (currentAlignment != null) { - indentationLevel= currentAlignment.breakIndentationLevel; - } - scanner.resetTo(currentTokenEndPosition, scannerEndPosition); - } - - private boolean printComment() { - return printComment(NO_TRAILING_COMMENT); - } - - /** - * Prints comment at the current position. - * - * @return {@code true} if a writespace character was encountered preceding the next token, - */ - private boolean printComment(int trailing) { - // If we have a space between two tokens we ensure it will be dumped in the formatted - // string. - int currentTokenStartPosition= scanner.getCurrentPosition(); - if (shouldSkip(currentTokenStartPosition)) { - return false; - } - boolean hasComment= false; - boolean hasLineComment= false; - boolean hasWhitespace= false; - char[] whiteSpaces= EMPTY_CHAR_ARRAY; - int lines= 0; - while ((currentToken= scanner.nextToken()) != null) { - if (skipOverInactive) { - Position inactivePos= getInactivePosAt(scanner.getCurrentTokenStartPosition()); - if (inactivePos != null) { - int startOffset= Math.min(scanner.getCurrentTokenStartPosition(), - inactivePos.getOffset()); - int endOffset= Math.min(scannerEndPosition, - inactivePos.getOffset() + inactivePos.getLength()); - if (startOffset < endOffset) { - int savedIndentLevel= indentationLevel; - scanner.resetTo(scanner.getCurrentTokenStartPosition(), scanner.eofPosition); - printRaw(startOffset, endOffset - startOffset); - while (indentationLevel > savedIndentLevel) { - unIndent(); - } - while (indentationLevel < savedIndentLevel) { - indent(); - } - scanner.resetTo(endOffset, scanner.eofPosition); - continue; - } - } - } - int tokenStartPosition = scanner.getCurrentTokenStartPosition(); - switch (currentToken.type) { - case Token.tWHITESPACE: - whiteSpaces= scanner.getCurrentTokenSource(); - int whitespacesEndPosition = scanner.getCurrentTokenEndPosition(); - lines= 0; - for (int i= 0, max= whiteSpaces.length; i < max; i++) { - switch (whiteSpaces[i]) { - case '\r': - if ((i + 1) < max) { - if (whiteSpaces[i + 1] == '\n') { - i++; - } - } - lines++; - break; - case '\n': - lines++; - } - } - // If following token is a line comment on the same line or the line just after, - // then it might be not really formatted as a trailing comment. - boolean realTrailing = trailing != NO_TRAILING_COMMENT; - if (realTrailing && scanner.peekNextChar() == '/' && lines == 0) { - boolean canChangeTrailing = false; - // For basic trailing comment preceded by a line comment, then it depends on - // the comments relative position when following comment column (after having - // been rounded) is below the preceding one, then it becomes not a good idea - // to change the trailing flag. - if (trailing == BASIC_TRAILING_COMMENT && hasLineComment) { - int currentCommentIndentation = computeIndentation(whiteSpaces, 0); - int relativeIndentation = - currentCommentIndentation - lastLineComment.currentIndentation; - if (tabLength == 0) { - canChangeTrailing = relativeIndentation == 0; - } else { - canChangeTrailing = relativeIndentation > -tabLength; - } - } - // If the trailing can be changed, then look at the following tokens. - if (canChangeTrailing) { - int currentTokenPosition = scanner.getCurrentTokenStartPosition(); - if (scanner.getNextToken() == Token.tLINECOMMENT) { - realTrailing = !hasLineComment; - switch (scanner.getNextToken()) { - case Token.tLINECOMMENT: - // At least two contiguous line comments. - // The formatter should not consider comments as trailing ones. - realTrailing = false; - break; - case Token.tWHITESPACE: - if (scanner.getNextToken() == Token.tLINECOMMENT) { - // At least two contiguous line comments. - // The formatter should not consider comments as trailing ones. - realTrailing = false; - } - break; - } - } - scanner.resetTo(currentTokenPosition, scanner.eofPosition); - scanner.getNextToken(); // Get current token again to restore the scanner state. - } - } - // Strategy to consume spaces and eventually leave at this stage - // depends on the fact that a trailing comment is expected or not - if (realTrailing) { - // If a line comment is consumed, no other comment can be on the same line after - if (hasLineComment) { - if (lines >= 1) { - currentTokenStartPosition = tokenStartPosition; - preserveEmptyLines(lines, currentTokenStartPosition); - addDeleteEdit(currentTokenStartPosition, whitespacesEndPosition); - scanner.resetTo(scanner.getCurrentPosition(), scannerEndPosition); - return hasWhitespace; - } - scanner.resetTo(currentTokenStartPosition, scannerEndPosition); - return hasWhitespace; - } - // If one or several new lines are consumed, following comments - // cannot be considered as trailing ones. - if (lines >= 1) { - if (hasComment) { - printNewLine(tokenStartPosition); - } - scanner.resetTo(currentTokenStartPosition, scannerEndPosition); - return hasWhitespace; - } - // Delete consumed white spaces - hasWhitespace = true; - currentTokenStartPosition = scanner.getCurrentPosition(); - addDeleteEdit(tokenStartPosition, whitespacesEndPosition); - } else { - if (lines == 0) { - hasWhitespace= true; - addDeleteEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition()); - } else if (hasComment) { - if (lines == 1) { - printNewLine(scanner.getCurrentTokenStartPosition()); - } else { - preserveEmptyLines(lines - 1, scanner.getCurrentTokenStartPosition()); - } - addDeleteEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition()); - } else if (hasLineComment) { - preserveEmptyLines(lines, scanner.getCurrentTokenStartPosition()); - addDeleteEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition()); - } else if (lines != 0 && (!preferences.join_wrapped_lines || preferences.number_of_empty_lines_to_preserve != 0)) { - String preservedEmptyLines= getPreserveEmptyLines(lines - 1); - addReplaceEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), - preservedEmptyLines); - hasWhitespace= preservedEmptyLines.length() == 0; - } else { - addDeleteEdit(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition()); - hasWhitespace= true; - } - } - currentTokenStartPosition= scanner.getCurrentPosition(); - break; - case Token.tLINECOMMENT: - if (lines >= 1) { - if (lines > 1) { - preserveEmptyLines(lines - 1, scanner.getCurrentTokenStartPosition()); - } else if (lines == 1) { - printNewLine(scanner.getCurrentTokenStartPosition()); - } - } else if (hasWhitespace) { - // Look whether comments line may be contiguous or not - // Note that when preceding token is a comment line, then only one line - // is enough to have an empty line as the line end is included in the comment line. - // If comments are contiguous, store the white spaces to be able to compute - // the current comment indentation - if (lines > 1 || (lines == 1 && hasLineComment)) { - lastLineComment.contiguous = false; - } - lastLineComment.leadingSpaces = whiteSpaces; - } - whiteSpaces= EMPTY_CHAR_ARRAY; - hasWhitespace= false; - printLineComment(); - currentTokenStartPosition= scanner.getCurrentPosition(); - hasLineComment= true; - lines= 0; - break; - case Token.tBLOCKCOMMENT: - if (lines >= 1) { - if (lines > 1) { - preserveEmptyLines(lines - 1, scanner.getCurrentTokenStartPosition()); - } else if (lines == 1) { - printNewLine(scanner.getCurrentTokenStartPosition()); - } - } else if (hasWhitespace) { - space(); - } - whiteSpaces= EMPTY_CHAR_ARRAY; - hasWhitespace= false; - printBlockComment(false); - currentTokenStartPosition= scanner.getCurrentPosition(); - hasLineComment= false; - hasComment= true; - lines= 0; - break; - case Token.tPREPROCESSOR: - case Token.tPREPROCESSOR_DEFINE: - case Token.tPREPROCESSOR_INCLUDE: - if (column != 1) - printNewLine(scanner.getCurrentTokenStartPosition()); - if (lines > 1) { - preserveEmptyLines(lines - 1, scanner.getCurrentTokenStartPosition()); - } - whiteSpaces= EMPTY_CHAR_ARRAY; - hasWhitespace= false; - printPreprocessorDirective(); - printNewLine(); - currentTokenStartPosition= scanner.getCurrentPosition(); - hasLineComment= false; - hasComment= false; - lines= 0; - break; - default: - // step back one token - scanner.resetTo(currentTokenStartPosition, scannerEndPosition); - return hasWhitespace; - } - } - scanner.resetTo(currentTokenStartPosition, scannerEndPosition); - return hasWhitespace; - } - - /** - * @param offset - * @return - */ - private Position getInactivePosAt(int offset) { - for (Iterator<Position> iter= fSkipPositions.iterator(); iter.hasNext();) { - Position pos= iter.next(); - if (pos.includes(offset)) { - return pos; - } - } - return null; - } - - private void printLineComment() { - int currentTokenStartPosition = scanner.getCurrentTokenStartPosition(); - int currentTokenEndPosition = scanner.getCurrentTokenEndPosition() + 1; - scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition); - int currentCharacter; - int start = currentTokenStartPosition; - int nextCharacterStart = currentTokenStartPosition; - - // Print comment line indentation - int commentIndentationLevel; - boolean onFirstColumn = isOnFirstColumn(start); - if (indentationLevel == 0) { - commentIndentationLevel = column - 1; - } else { - if (onFirstColumn && preferences.never_indent_line_comments_on_first_column) { - commentIndentationLevel = column - 1; - } else { - // Indentation may be specific for contiguous comment - // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=293300 - if (lastLineComment.contiguous) { - // The leading spaces have been set while looping in the printComment(int) method - int currentCommentIndentation = computeIndentation(lastLineComment.leadingSpaces, 0); - // Keep the current comment indentation when over the previous contiguous line comment - // and the previous comment has not been re-indented - int relativeIndentation = currentCommentIndentation - lastLineComment.currentIndentation; - boolean similarCommentsIndentation = false; - if (tabLength == 0) { - similarCommentsIndentation = relativeIndentation == 0; - } else if (relativeIndentation > -tabLength) { - similarCommentsIndentation = relativeIndentation == 0 - || currentCommentIndentation != 0 && lastLineComment.currentIndentation != 0; - } - if (similarCommentsIndentation && lastLineComment.indentation != indentationLevel) { - int currentIndentationLevel = indentationLevel; - indentationLevel = lastLineComment.indentation; - printIndentationIfNecessary(); - indentationLevel = currentIndentationLevel; - commentIndentationLevel = lastLineComment.indentation; - } else { - printIndentationIfNecessary(); - commentIndentationLevel = column - 1; - } - } else { - printIndentationIfNecessary(); - commentIndentationLevel = column - 1; - } - } - } - - // Prepare white space before the comment. - StringBuilder whitespace = null; - if (!lastLineComment.contiguous && commentIndentationLevel != indentationLevel && - !(preferences.never_indent_line_comments_on_first_column && onFirstColumn)) { - whitespace = new StringBuilder(); - int whitespaceStartPosition = currentTokenStartPosition - lastLineComment.leadingSpaces.length; - int indent = getIndentationOfOffset(whitespaceStartPosition); - int distance = computeIndentation(lastLineComment.leadingSpaces, indent) - indent; - if (preferences.comment_preserve_white_space_between_code_and_line_comment && - distance >= preferences.comment_min_distance_between_code_and_line_comment) { - whitespace.append(lastLineComment.leadingSpaces); - } else { - for (int i = 0; i < preferences.comment_min_distance_between_code_and_line_comment; i++) { - whitespace.append(' '); - } - } - } - - // Store line comment information - lastLineComment.currentIndentation = getIndentationOfOffset(currentTokenStartPosition); - lastLineComment.contiguous = true; - - while (true) { - STPLocation location = new STPLocation(this, scanner.getCurrentPosition()); - int commentIndent = commentIndentationLevel; - - // Add pending space if necessary - if (whitespace != null) { - addInsertEdit(currentTokenStartPosition, whitespace); - commentIndent = computeIndentation(whitespace, commentIndentationLevel); - needSpace = false; - pendingSpace = false; - } - lastLineComment.indentation = commentIndent; - - int previousStart = currentTokenStartPosition; - - int indent = commentIndent; - loop: while (nextCharacterStart <= currentTokenEndPosition && - (currentCharacter = scanner.getNextChar()) != -1) { - nextCharacterStart = scanner.getCurrentPosition(); - - switch (currentCharacter) { - case '\r': - case '\n': - start = previousStart; - break loop; - } - indent = computeIndentation((char) currentCharacter, indent); - previousStart = nextCharacterStart; - } - - if (start != currentTokenStartPosition) { - // This means that the line comment doesn't end the file - addReplaceEdit(start, currentTokenEndPosition - 1, lineSeparator); - line++; - column = 1; - lastNumberOfNewLines = 1; - } - if (!checkLineWrapping || indent <= pageWidth || whitespace == null || - commentIndent - commentIndentationLevel <= preferences.comment_min_distance_between_code_and_line_comment) { - break; - } - - // Maximum line length was exceeded. Try to reduce white space before the comment by - // removing the last white space character. - whitespace.deleteCharAt(whitespace.length() - 1); - if (computeIndentation(lastLineComment.leadingSpaces, commentIndentationLevel) - commentIndentationLevel < - preferences.comment_min_distance_between_code_and_line_comment) { - // The white space shrank too much. Rebuild it to satisfy the minimum distance - // requirement. - whitespace.delete(0, whitespace.length()); - for (int i = 0; i < preferences.comment_min_distance_between_code_and_line_comment; i++) { - whitespace.append(' '); - } - } - resetAt(location); - scanner.resetTo(location.inputOffset, scanner.eofPosition); - } - - needSpace = false; - pendingSpace = false; - // realign to the proper value - if (currentAlignment != null) { - if (memberAlignment != null) { - // select the last alignment - if (currentAlignment.location.inputOffset > memberAlignment.location.inputOffset) { - if (currentAlignment.couldBreak() && currentAlignment.wasSplit) { - currentAlignment.performFragmentEffect(); - } - } else { - indentationLevel = Math.max(indentationLevel, memberAlignment.breakIndentationLevel); - } - } else if (currentAlignment.couldBreak() && currentAlignment.wasSplit) { - currentAlignment.performFragmentEffect(); - } - if (currentAlignment.name.equals(STPAlignment.BINARY_EXPRESSION) && - currentAlignment.enclosing != null && - currentAlignment.enclosing.equals(STPAlignment.BINARY_EXPRESSION) && - indentationLevel < currentAlignment.breakIndentationLevel) { - indentationLevel = currentAlignment.breakIndentationLevel; - } - } - scanner.resetTo(currentTokenEndPosition, scannerEndPosition); - } - - private void printEmptyLines(int linesNumber, int insertPosition) { - final String buffer= getEmptyLines(linesNumber); - if (!buffer.isEmpty()) { - addInsertEdit(insertPosition, buffer); - } - } - - private void printIndentationIfNecessary() { - if (column > indentationLevel) - return; - StringBuilder buffer= new StringBuilder(); - printIndentationIfNecessary(buffer); - if (buffer.length() > 0) { - addInsertEdit(scanner.getCurrentTokenStartPosition(), buffer); - pendingSpace= false; - } - } - - private void printIndentationIfNecessary(StringBuilder buffer) { - switch (tabChar) { - case STPDefaultCodeFormatterOptions.TAB: - boolean useTabsForLeadingIndents= useTabsOnlyForLeadingIndents; - int numberOfLeadingIndents= numberOfIndentations; - int indentationsAsTab= 0; - if (useTabsForLeadingIndents) { - while (column <= indentationLevel) { - if (indentationsAsTab < numberOfLeadingIndents) { - buffer.append('\t'); - indentationsAsTab++; - int complement= tabLength - ((column - 1) % tabLength); // amount of space - column += complement; - needSpace= false; - } else { - buffer.append(' '); - column++; - needSpace= false; - } - } - } else { - while (column <= indentationLevel - indentationLevel % tabLength) { - buffer.append('\t'); - int complement= tabLength - ((column - 1) % tabLength); // amount of space - column += complement; - needSpace= false; - } - while (column <= indentationLevel) { - buffer.append(' '); - column++; - needSpace= false; - } - } - break; - case STPDefaultCodeFormatterOptions.SPACE: - while (column <= indentationLevel) { - buffer.append(' '); - column++; - needSpace= false; - } - break; - case STPDefaultCodeFormatterOptions.MIXED: - useTabsForLeadingIndents= useTabsOnlyForLeadingIndents; - numberOfLeadingIndents= numberOfIndentations; - indentationsAsTab= 0; - if (useTabsForLeadingIndents) { - final int columnForLeadingIndents= numberOfLeadingIndents * indentationSize; - while (column <= indentationLevel) { - if (column <= columnForLeadingIndents) { - if ((column - 1 + tabLength) <= indentationLevel) { - buffer.append('\t'); - column += tabLength; - } else if ((column - 1 + indentationSize) <= indentationLevel) { - // print one indentation - for (int i= 0, max= indentationSize; i < max; i++) { - buffer.append(' '); - column++; - } - } else { - buffer.append(' '); - column++; - } - } else { - for (int i= column, max= indentationLevel; i <= max; i++) { - buffer.append(' '); - column++; - } - } - needSpace= false; - } - } else { - while (column <= indentationLevel) { - if ((column - 1 + tabLength) <= indentationLevel) { - buffer.append('\t'); - column += tabLength; - } else if ((column - 1 + indentationSize) <= indentationLevel) { - // print one indentation - for (int i= 0, max= indentationSize; i < max; i++) { - buffer.append(' '); - column++; - } - } else { - buffer.append(' '); - column++; - } - needSpace= false; - } - } - break; - } - } - - public void startNewLine() { - if (column > 1) { - printNewLine(); - } - } - - private void printNewLine() { - printNewLine(scanner.getCurrentPosition()); - } - - private void printNewLine(int insertPosition) { - if (shouldSkip(scanner.getCurrentPosition())) { - return; - } - if (lastNumberOfNewLines >= 1) { - // Ensure that the scribe is at the beginning of a new line - // only if no specific indentation has been previously set. - if (!preserveLineBreakIndentation) { - column = 1; - } - preserveLineBreakIndentation = false; - return; - } - addInsertEdit(insertPosition, lineSeparator); - line++; - lastNumberOfNewLines= 1; - column= 1; - needSpace= false; - pendingSpace= false; - preserveLineBreakIndentation = false; - lastLineComment.contiguous = false; - } - - private void printNextToken(int expectedTokenType) { - printNextToken(expectedTokenType, false); - } - - private void printNextToken(int expectedTokenType, boolean considerSpaceIfAny) { - // Set brace flag, it's useful for the scribe while preserving line breaks - switch (expectedTokenType) { - case Token.tRBRACE: - case Token.tLBRACE: - formatBrace = true; - lastLineComment.contiguous = false; - } - try { - printComment(); - if (shouldSkip(scanner.getCurrentPosition())) { - return; - } - currentToken= scanner.nextToken(); - if (currentToken == null || expectedTokenType != currentToken.type) { - if (pendingSpace) { - addInsertEdit(scanner.getCurrentTokenStartPosition(), SPACE); - } - pendingSpace= false; - needSpace= true; - throw new AbortFormatting( - "[" + (line + 1) + "/" + column + "] Unexpected token type, expecting:" + //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ - expectedTokenType + ", actual:" + currentToken);//$NON-NLS-1$ - } - print(currentToken.getLength(), considerSpaceIfAny); - } finally { - formatBrace = false; - } - } - - private void printRuler(StringBuilder stringBuffer) { - for (int i= 0; i < pageWidth; i++) { - if ((i % tabLength) == 0) { - stringBuffer.append('+'); - } else { - stringBuffer.append('-'); - } - } - stringBuffer.append(lineSeparator); - - for (int i= 0; i < (pageWidth / tabLength); i++) { - stringBuffer.append(i); - stringBuffer.append('\t'); - } - } - - private void printTrailingComment() { - printComment(BASIC_TRAILING_COMMENT); - } - - private void reset() { - checkLineWrapping= true; - line= 0; - column= 1; - editsIndex= 0; - } - - private void resetAt(STPLocation location) { - line= location.outputLine; - column= location.outputColumn; - indentationLevel= location.outputIndentationLevel; - needSpace= location.needSpace; - pendingSpace= location.pendingSpace; - numberOfIndentations= location.numberOfIndentations; - lastNumberOfNewLines= location.lastNumberOfNewLines; - editsIndex= location.editsIndex; - if (editsIndex > 0) { - edits[editsIndex - 1]= location.textEdit; - } - setTailFormatter(location.tailFormatter); - } - - private void resize() { - System.arraycopy(edits, 0, (edits= new OptimizedReplaceEdit[editsIndex * 2]), 0, editsIndex); - } - - private void space() { - if (!needSpace) - return; - if (shouldSkip(scanner.getCurrentPosition())) { - return; - } - lastNumberOfNewLines= 0; - pendingSpace= true; - column++; - needSpace= false; - } - - @Override - public String toString() { - StringBuilder buffer= new StringBuilder(); - buffer.append("(page width = " + pageWidth + ") - (tabChar = "); //$NON-NLS-1$//$NON-NLS-2$ - switch (tabChar) { - case STPDefaultCodeFormatterOptions.TAB: - buffer.append("TAB"); //$NON-NLS-1$ - break; - case STPDefaultCodeFormatterOptions.SPACE: - buffer.append("SPACE"); //$NON-NLS-1$ - break; - default: - buffer.append("MIXED"); //$NON-NLS-1$ - } - buffer - .append(") - (tabSize = " + tabLength + ")") //$NON-NLS-1$//$NON-NLS-2$ - .append(lineSeparator) - .append("(line = " + line + ") - (column = " + column + ") - (identationLevel = " + indentationLevel + ")") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - .append(lineSeparator) - .append("(needSpace = " + needSpace + ") - (lastNumberOfNewLines = " + lastNumberOfNewLines + ") - (checkLineWrapping = " + checkLineWrapping + ")") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - .append(lineSeparator).append( - "==================================================================================") //$NON-NLS-1$ - .append(lineSeparator); - printRuler(buffer); - return buffer.toString(); - } - - private void unIndent() { - if (shouldSkip(scanner.getCurrentPosition())) { - return; - } - indentationLevel-= indentationSize; - numberOfIndentations--; - } - - - private boolean shouldSkip(int offset) { - return offset >= fSkipStartOffset && offset < fSkipEndOffset; - } - - /* - * Returns the tail formatter associated with the current alignment or, if there is no current - * alignment, with the scribe itself. - * @see #tailFormatter - */ - public Runnable getTailFormatter() { - if (currentAlignment != null) { - return currentAlignment.tailFormatter; - } else { - return this.tailFormatter; - } - } - - /* - * Sets the tail formatter associated with the current alignment or, if there is no current - * alignment, with the scribe itself. - * @see #tailFormatter - */ - private void setTailFormatter(Runnable tailFormatter) { - if (currentAlignment != null) { - currentAlignment.tailFormatter = tailFormatter; - } else { - this.tailFormatter = tailFormatter; - } - } -} diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/Scanner.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/Scanner.java deleted file mode 100644 index 6e073903ef..0000000000 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/Scanner.java +++ /dev/null @@ -1,224 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2006, 2011, 2013 Wind River Systems, Inc. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Anton Leherbauer (Wind River Systems) - initial API and implementation - * Sergey Prigogin (Google) - * Red Hat Inc. - modified for use in SystemTap - *******************************************************************************/ -package org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp; - -import java.io.CharArrayReader; -import java.io.Reader; - -/** - * A scanner operating on a character array and allowing to reposition the scanner. - * - * @since 4.0 - */ -public class Scanner extends SimpleScanner { - public char[] source; - public int eofPosition; - public int startPosition; - - public Scanner() { - setReuseToken(true); - setSplitPreprocessor(false); - } - - @Override - protected void init(Reader reader) { - // not allowed - throw new UnsupportedOperationException(); - } - - /** - * Set the source text as character array. - * - * @param source the source text - */ - public void setSource(char[] source) { - this.source= source; - fContext= new ScannerContext().initialize(new CharArrayReader(source)); - startPosition= -1; - eofPosition= source.length; - } - - /** - * Reset scanner to given inclusive start and end offsets - * @param start inclusive start offset - * @param end exclusive end offset - */ - public void resetTo(int start, int end) { - Reader reader; - if (end >= source.length) { - reader= new CharArrayReader(source); - } else { - reader= new CharArrayReader(source, 0, Math.min(source.length, end)); - } - fContext= new ScannerContext().initialize(reader, start); - startPosition= start; - if (source != null && source.length < end) { - eofPosition = source.length; - } else { - eofPosition = end; - } - } - - /** - * Get the start offset of the current token. - * @return the start offset of the current token - */ - public int getCurrentTokenStartPosition() { - return fCurrentToken.offset; - } - - /** - * Get the inclusive end offset of the current token. - * @return the inclusive end offset of the current token - */ - public int getCurrentTokenEndPosition() { - return getCurrentPosition() - 1; - } - - /** - * Get the current scanner offset. - * @return the current scanner offset - */ - public int getCurrentPosition() { - return fContext.getOffset() - fContext.undoStackSize(); - } - - /** - * Returns {@code true} if the scanner has reached the end of file. - */ - public final boolean atEnd() { - return getCurrentPosition() >= eofPosition; - } - - /** - * Get the next character. - * @return the next character - */ - public int getNextChar() { - return getChar(); - } - - /** - * Move to next character iff it is equal to the given expected character. - * If the characters do not match, the scanner does not move forward. - * - * @param c the expected character - * @return <code>true</code> if the next character was the expected character - */ - public boolean getNextChar(char c) { - if (c == getChar()) { - return true; - } - ungetChar(c); - return false; - } - - /** - * Returns the next character without moving the pointer. - */ - public int peekNextChar() { - int c = getChar(); - ungetChar(c); - return c; - } - - /** - * Set current scanner offset to given offset. - * - * @param nextCharacterStart the desired scanner offset - */ - public void setCurrentPosition(int nextCharacterStart) { - int currentPos= getCurrentPosition(); - int diff= currentPos - nextCharacterStart; - if (diff < 0) { - do { - getChar(); - ++diff; - } while (diff < 0); - } else if (diff == 0) { - // no-op - } else if (diff > fTokenBuffer.length()) { - resetTo(nextCharacterStart, source.length); - } else { - while (diff > 0) { - if (fTokenBuffer.length() > 0) { - ungetChar(fTokenBuffer.charAt(fTokenBuffer.length() - 1)); - } - --diff; - } - } - } - - /** - * Get the text of the current token as a character array. - * @return the token text - */ - public char[] getCurrentTokenSource() { - return fCurrentToken.getText().toCharArray(); - } - - /** - * Get the next token as token type constant. - * - * @return the next token type - */ - public int getNextToken() { - Token token= nextToken(); - if (token == null) { - return -1; - } - return token.type; - } - - /** - * For debugging purposes. - */ - @Override - public String toString() { - if (this.startPosition == this.source.length) - return "EOF\n\n" + new String(this.source); //$NON-NLS-1$ - if (this.getCurrentPosition() > this.source.length) - return "behind the EOF\n\n" + new String(this.source); //$NON-NLS-1$ - - char front[] = new char[this.startPosition]; - System.arraycopy(this.source, 0, front, 0, this.startPosition); - - int middleLength = (this.getCurrentPosition() - 1) - this.startPosition + 1; - char middle[]; - if (middleLength > -1) { - middle = new char[middleLength]; - System.arraycopy( - this.source, - this.startPosition, - middle, - 0, - middleLength); - } else { - middle = new char[0]; - } - - char end[] = new char[this.source.length - (this.getCurrentPosition() - 1)]; - System.arraycopy( - this.source, - (this.getCurrentPosition() - 1) + 1, - end, - 0, - this.source.length - (this.getCurrentPosition() - 1) - 1); - - return new String(front) - + "\n===============================\nStarts here -->" //$NON-NLS-1$ - + new String(middle) - + "<-- Ends here\n===============================\n" //$NON-NLS-1$ - + new String(end); - } -} diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/ScannerContext.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/ScannerContext.java index d4d72fd372..e4f157f4e5 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/ScannerContext.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/ScannerContext.java @@ -31,17 +31,6 @@ public class ScannerContext { return this; } - public ScannerContext initialize(Reader r, int offset) { - try { - r.skip(offset); - } catch (IOException exc) { - throw new RuntimeException(exc); - } - fReader = r; - fOffset = offset; - return this; - } - public int read() throws IOException { ++fOffset; return fReader.read(); @@ -55,14 +44,6 @@ public class ScannerContext { return fOffset; } - /** - * Returns the reader. - * @return Reader - */ - public final Reader getReader() { - return fReader; - } - public final int undoStackSize() { return fUndo.size(); } diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/SimpleScanner.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/SimpleScanner.java index cc04f1dd68..3e814cc93e 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/SimpleScanner.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/editors/stp/SimpleScanner.java @@ -30,90 +30,12 @@ public class SimpleScanner { protected Token fCurrentToken; protected ScannerContext fContext; protected StringBuilder fTokenBuffer= new StringBuilder(); - private int fPreprocessorToken= 0; - private boolean fReuseToken; - private boolean fSplitPreprocessor; private final StringBuilder fUniversalCharBuffer= new StringBuilder(); - public SimpleScanner() { - super(); - } - - public void setReuseToken(boolean val) { - fReuseToken= val; - if (val) { - fCurrentToken= new Token(0, null); - } - } - - public void setSplitPreprocessor(boolean val) { - fSplitPreprocessor= val; - } - protected void init(Reader reader) { - fReuseToken= false; - fSplitPreprocessor= true; - fPreprocessorToken= 0; fContext = new ScannerContext().initialize(reader); } - public SimpleScanner initialize(Reader reader) { - init(reader); - return this; - } - - private final void setCurrentToken(Token t) { - fCurrentToken = t; - } - - private final Token newToken(int t) { - if (!fReuseToken) { - setCurrentToken(new Token(t, fTokenBuffer.toString(), fContext)); - } else { - fCurrentToken.set(t, fTokenBuffer.toString(), fContext); - } - return fCurrentToken; - } - - private Token newPreprocessorToken() { - if (fPreprocessorToken==0) { - fPreprocessorToken= categorizePreprocessor(fTokenBuffer); - } - return newToken(fPreprocessorToken); - } - - private int categorizePreprocessor(StringBuilder text) { - boolean skipHash= true; - int i= 0; - for (; i < text.length(); i++) { - char c= text.charAt(i); - if (!Character.isWhitespace(c)) { - if (!skipHash) { - break; - } - skipHash= false; - if (c != '#') { - break; - } - } - } - String innerText= text.substring(i); - if (innerText.startsWith("include")) { //$NON-NLS-1$ - return Token.tPREPROCESSOR_INCLUDE; - } - if (innerText.startsWith("define")) { //$NON-NLS-1$ - return Token.tPREPROCESSOR_DEFINE; - } - if (innerText.startsWith("undef")) { //$NON-NLS-1$ - return Token.tPREPROCESSOR_DEFINE; - } - return Token.tPREPROCESSOR; - } - - protected final int getChar() { - return getChar(false); - } - private int getChar(boolean insideString) { int c = EOFCHAR; @@ -200,7 +122,7 @@ public class SimpleScanner { fContext.pushUndo(c); } - protected void ungetChar(int c) { + private void ungetChar(int c) { if (c < 256 || c == fTokenBuffer.charAt(fTokenBuffer.length() - 1)) { internalUngetChar(c); } else if (fUniversalCharBuffer.length() > 0) { @@ -213,652 +135,6 @@ public class SimpleScanner { } } - public Token nextToken() { - fTokenBuffer.setLength(0); - - boolean madeMistake = false; - int c = getChar(); - - while (c != EOFCHAR) { - if (fPreprocessorToken != 0) { - Token token= continuePPDirective(c); - if (token != null) { - return token; - } - } - - if ((c == ' ') || (c == '\r') || (c == '\t') || (c == '\n')) { - do { - c = getChar(); - } while ((c == ' ') || (c == '\r') || (c == '\t') || (c == '\n')); - ungetChar(c); - return newToken(Token.tWHITESPACE); - } else if (c == '"') { - matchStringLiteral(); - return newToken(Token.tSTRING); - } else if (c == 'L' && !madeMistake) { - int oldChar = c; - c = getChar(); - if (c != '"') { - // we have made a mistake - ungetChar(c); - c = oldChar; - madeMistake = true; - continue; - } - - matchStringLiteral(); - return newToken(Token.tLSTRING); - } else if (c == 'R' && !madeMistake) { - int oldChar = c; - c = getChar(); - if (c != '"') { - // we have made a mistake - ungetChar(c); - c = oldChar; - madeMistake = true; - continue; - } - - matchRawStringLiteral(); - return newToken(Token.tRSTRING); - } else if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || (c == '_') || (c > 255 && Character.isUnicodeIdentifierStart(c))) { - madeMistake = false; - - c = getChar(); - - while (((c >= 'a') && (c <= 'z')) - || ((c >= 'A') && (c <= 'Z')) - || ((c >= '0') && (c <= '9')) - || (c == '_') - || (c > 255 && Character.isUnicodeIdentifierPart(c))) { - c = getChar(); - } - - ungetChar(c); - - String ident = fTokenBuffer.toString(); - - Object tokenTypeObject; - - tokenTypeObject = fgKeywords.get(ident); - - int tokenType = Token.tIDENTIFIER; - if (tokenTypeObject != null) - tokenType = ((Integer)tokenTypeObject).intValue(); - - return newToken(tokenType); - } else if ((c >= '0') && (c <= '9') || c == '.') { - boolean hex = false; - boolean floatingPoint = c == '.'; - boolean firstCharZero = c == '0'; - - c = getChar(); - - if (firstCharZero && c == 'x') { - hex = true; - c = getChar(); - } - - int digits= 0; - - while ((c >= '0' && c <= '9') || (hex && ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))) { - ++digits; - c = getChar(); - } - - if (!hex) { - if (c == '*') { - if (floatingPoint && digits == 0) { - // encountered .* - return newToken(Token.tDOTSTAR); - } - } else if (c == '.') { - if (floatingPoint && digits == 0) { - // encountered .. - if ((c= getChar()) == '.') { - return newToken(Token.tELIPSE); - } else { - ungetChar(c); - ungetChar('.'); - return newToken(Token.tDOT); - } - } - - floatingPoint = true; - c = getChar(); - while ((c >= '0' && c <= '9')) { - ++digits; - c = getChar(); - } - } else if (digits > 0 && (c == 'e' || c == 'E')) { - floatingPoint = true; - - // exponent type for floating point - c = getChar(); - - // optional + or - - if (c == '+' || c == '-') { - c = getChar(); - } - - // digit sequence of exponent part - while ((c >= '0' && c <= '9')) { - c = getChar(); - } - } - } - if (floatingPoint) { - if (digits > 0) { - //floating-suffix - if (c == 'l' || c == 'L' || c == 'f' || c == 'F') { - c = getChar(); - } - } else { - ungetChar(c); - return newToken(Token.tDOT); - } - } else { - //integer suffix - if (c == 'u' || c == 'U') { - c = getChar(); - if (c == 'l' || c == 'L') - c = getChar(); - } else if (c == 'l' || c == 'L') { - c = getChar(); - if (c == 'u' || c == 'U') - c = getChar(); - } - } - - ungetChar(c); - - int tokenType; - String result = fTokenBuffer.toString(); - - if (floatingPoint && result.equals(".")) //$NON-NLS-1$ - tokenType = Token.tDOT; - else - tokenType = floatingPoint ? Token.tFLOATINGPT : Token.tINTEGER; - - return newToken(tokenType); - } else if (c == '#') { - return matchPPDirective(); - } else { - switch (c) { - case '\'': - matchCharLiteral(); - return newToken(Token.tCHAR); - - case ':': - c = getChar(); - if (c == ':') { - return newToken(Token.tCOLONCOLON); - } else { - ungetChar(c); - return newToken(Token.tCOLON); - } - case ';': - return newToken(Token.tSEMI); - case ',': - return newToken(Token.tCOMMA); - case '?': - return newToken(Token.tQUESTION); - case '(': - return newToken(Token.tLPAREN); - case ')': - return newToken(Token.tRPAREN); - case '[': - return newToken(Token.tLBRACKET); - case ']': - return newToken(Token.tRBRACKET); - case '{': - return newToken(Token.tLBRACE); - case '}': - return newToken(Token.tRBRACE); - case '+': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tPLUSASSIGN); - case '+': - return newToken(Token.tINCR); - default: - ungetChar(c); - return newToken(Token.tPLUS); - } - case '-': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tMINUSASSIGN); - case '-': - return newToken(Token.tDECR); - case '>': - c = getChar(); - switch (c) { - case '*': - return newToken(Token.tARROWSTAR); - default: - ungetChar(c); - return newToken(Token.tARROW); - } - default: - ungetChar(c); - return newToken(Token.tMINUS); - } - case '*': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tSTARASSIGN); - default: - ungetChar(c); - return newToken(Token.tSTAR); - } - case '%': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tMODASSIGN); - default: - ungetChar(c); - return newToken(Token.tMOD); - } - case '^': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tXORASSIGN); - default: - ungetChar(c); - return newToken(Token.tXOR); - } - case '&': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tAMPERASSIGN); - case '&': - return newToken(Token.tAND); - default: - ungetChar(c); - return newToken(Token.tAMPER); - } - case '|': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tBITORASSIGN); - case '|': - return newToken(Token.tOR); - default: - ungetChar(c); - return newToken(Token.tBITOR); - } - case '~': - return newToken(Token.tCOMPL); - case '!': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tNOTEQUAL); - default: - ungetChar(c); - return newToken(Token.tNOT); - } - case '=': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tEQUAL); - default: - ungetChar(c); - return newToken(Token.tASSIGN); - } - case '<': - c = getChar(); - switch (c) { - case '<': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tSHIFTLASSIGN); - default: - ungetChar(c); - return newToken(Token.tSHIFTL); - } - case '=': - return newToken(Token.tLTEQUAL); - default: - ungetChar(c); - return newToken(Token.tLT); - } - case '>': - c = getChar(); - switch (c) { - case '>': - c = getChar(); - switch (c) { - case '=': - return newToken(Token.tSHIFTRASSIGN); - default: - ungetChar(c); - return newToken(Token.tSHIFTR); - } - case '=': - return newToken(Token.tGTEQUAL); - default: - ungetChar(c); - return newToken(Token.tGT); - } - case '.': - c = getChar(); - switch (c) { - case '.': - c = getChar(); - switch (c) { - case '.': - return newToken(Token.tELIPSE); - default: - break; - } - break; - case '*': - return newToken(Token.tDOTSTAR); - default: - ungetChar(c); - return newToken(Token.tDOT); - } - break; - case '/': - c = getChar(); - switch (c) { - case '/': { - matchSinglelineComment(); - return newToken(Token.tLINECOMMENT); - } - case '*': { - matchMultilineComment(); - return newToken(Token.tBLOCKCOMMENT); - } - case '=': - return newToken(Token.tDIVASSIGN); - default: - ungetChar(c); - return newToken(Token.tDIV); - } - default: - // Bad character - return newToken(Token.tBADCHAR); - } - // throw EOF; - } - } - - // we're done - // throw EOF; - return null; - } - - private void matchCharLiteral() { - int c; - c = getChar(true); - int next = getChar(true); - if (c == '\\') { - if (next >= '0' && next <= '7') { - do { - next = getChar(true); - } while (next >= '0' && next <= '7'); - } else if (next == 'x' || next == 'X' || next == 'u' || next == 'U') { - do { - next = getChar(true); - } while ((next >= '0' && next <= '9') || (next >= 'a' && next <= 'f') - || (next >= 'A' && next <= 'F')); - } else { - next = getChar(true); - } - } - if (next != '\'') { - ungetChar(next); - } - } - - private void matchStringLiteral() { - // string - boolean escaped= false; - int c = getChar(true); - - LOOP: for (;;) { - if (c == EOFCHAR) - break; - if (escaped) { - escaped= false; - int nc= getChar(true); - if (c=='\r' && nc=='\n') { - nc= getChar(true); - } - c= nc; - } else { - switch(c) { - case '\\': - escaped= true; - break; - case '"': - break LOOP; - case '\r': - case '\n': - // unterminated string constant - ungetChar(c); - break LOOP; - } - c = getChar(true); - } - } - } - - private void matchRawStringLiteral() { - // raw-string R"<delim-opt>(string)<delim-opt>"; - int c = getChar(false); - StringBuilder delim = new StringBuilder(12); - while (c != '(') { - if (c == EOFCHAR) { - return; - } - delim.append((char) c); - c = getChar(false); - } - int delimLen = delim.length(); - c = getChar(false); - LOOP: - for (;;) { - if (c == EOFCHAR) - break; - if (c == ')') { - c = getChar(false); - int idx = 0; - while (idx < delimLen) { - if (c != delim.charAt(idx)) { - continue LOOP; - } - ++idx; - c = getChar(false); - } - if (c == '"') - break; - } - c = getChar(false); - } - } - - /** - * Matches a preprocessor directive. - * - * @return a preprocessor token - */ - private Token matchPPDirective() { - if (!fSplitPreprocessor) { - getRestOfPreprocessorLine(); - return newToken(Token.tPREPROCESSOR); - } - return continuePPDirective(getChar()); - } - - private Token continuePPDirective(int c) { - boolean done= false; - while (!done) { - switch(c) { - case '\'': - if (fTokenBuffer.length() > 1) { - if (fPreprocessorToken == 0) { - fPreprocessorToken= categorizePreprocessor(fTokenBuffer); - } - ungetChar(c); - return newPreprocessorToken(); - } - matchCharLiteral(); - return newToken(Token.tCHAR); - - case '"': - if (fTokenBuffer.length() > 1) { - if (fPreprocessorToken==0) { - fPreprocessorToken= categorizePreprocessor(fTokenBuffer); - } - if (fPreprocessorToken==Token.tPREPROCESSOR_INCLUDE) { - matchStringLiteral(); - c= getChar(); - break; - } else { - ungetChar(c); - return newPreprocessorToken(); - } - } - matchStringLiteral(); - return newToken(Token.tSTRING); - case '/': { - int next = getChar(); - if (next == '/') { - Token result= null; - if (fTokenBuffer.length() > 2) { - ungetChar(next); - ungetChar(c); - result= newPreprocessorToken(); - } else { - matchSinglelineComment(); - result= newToken(Token.tLINECOMMENT); - } - fPreprocessorToken= 0; - return result; - } - if (next == '*') { - if (fTokenBuffer.length() > 2) { - ungetChar(next); - ungetChar(c); - return newPreprocessorToken(); - } - // multiline comment - if (matchMultilineComment()) { - fPreprocessorToken= 0; - } - return newToken(Token.tBLOCKCOMMENT); - } - c = next; - break; - } - case '\n': - case '\r': - case EOFCHAR: - done= true; - break; - default: - c= getChar(); - break; - } - } - ungetChar(c); - Token result= null; - if (fTokenBuffer.length() > 0) { - result= newPreprocessorToken(); - } - fPreprocessorToken= 0; - return result; - } - - /** - * Read until end of preprocessor directive. - */ - private void getRestOfPreprocessorLine() { - int c = getChar(); - while (true) { - while ((c != '\n') && (c != '\r') && (c != '/') && (c != '"') && (c != EOFCHAR)) { - c = getChar(); - } - if (c == '/') { - // we need to peek ahead at the next character to see if - // this is a comment or not - int next = getChar(); - if (next == '/') { - // single line comment - matchSinglelineComment(); - break; - } else if (next == '*') { - // multiline comment - if (matchMultilineComment()) - break; - else - c = getChar(); - continue; - } else { - // we are not in a comment - c = next; - continue; - } - } else if (c == '"') { - matchStringLiteral(); - c = getChar(); - } else { - ungetChar(c); - break; - } - } - } - - private void matchSinglelineComment() { - int c = getChar(); - while (c != '\n' && c != EOFCHAR) { - c = getChar(); - } - if (c == EOFCHAR) { - ungetChar(c); - } - } - - private boolean matchMultilineComment() { - boolean encounteredNewline = false; - int state = 0; - int c = getChar(); - while (state != 2 && c != EOFCHAR) { - if (c == '\n') - encounteredNewline = true; - - switch (state) { - case 0 : - if (c == '*') - state = 1; - break; - case 1 : - if (c == '/') - state = 2; - else if (c != '*') - state = 0; - break; - } - c = getChar(); - } - ungetChar(c); - return encounteredNewline; - } - static { fgKeywords.put("and", new Integer(Token.t_and)); //$NON-NLS-1$ fgKeywords.put("and_eq", new Integer(Token.t_and_eq)); //$NON-NLS-1$ |