Package sc.parser

Class ParseUtil

  • public class ParseUtil
    extends java.lang.Object
    Static utility methods used in the parsing code
    • Field Detail


        public static final java.lang.Object CONVERT_MISMATCH
    • Method Detail

      • stripComments

        public static java.lang.String stripComments​(java.lang.String str)
      • wrapArg

        public static java.lang.Object[] wrapArg​(java.lang.Object arg)
      • wrapArgs

        public static java.lang.Object[] wrapArgs​(java.lang.Object arg1,
                                                  java.lang.Object arg2)
      • escapeString

        public static java.lang.String escapeString​(IString s)
      • escapeString

        public static java.lang.String escapeString​(java.lang.String s)
      • getResultAsString

        public static java.lang.String getResultAsString​(java.lang.Object result)
      • readFileString

        public static java.lang.String readFileString​( file)
      • getParseNodeLineNumber

        public static int getParseNodeLineNumber​( file,
                                                 IParseNode node)
      • charOffsetToLineNumber

        public static int charOffsetToLineNumber​( file,
                                                 int charOffset)
      • charOffsetToLineNumber

        public static int charOffsetToLineNumber​(java.lang.String s,
                                                 int charOffset)
      • charOffsetToLine

        public static java.lang.String charOffsetToLine​( file,
                                                        int charOffset)
      • charOffsetToLinePos

        public static FilePosition charOffsetToLinePos​( file,
                                                       int charOffset)
      • charOffsetToLine

        public static java.lang.String charOffsetToLine​(java.lang.String s,
                                                        int charOffset)
      • charOffsetToLinePos

        public static FilePosition charOffsetToLinePos​(java.lang.String s,
                                                       int charOffset)
      • getIndentColumnForLine

        public static int getIndentColumnForLine​(java.lang.String fileName,
                                                 int lineNum)
        Returns the number of spaces for the given line for the given file name. Uses line numbers starting at 1. Warning - no caching or indexing - reads the file, counts the chars so don't put this into a loop. Returns 0 if the line number is found
      • findCharSequenceStartIndex

        public static int findCharSequenceStartIndex​(IParseNode pn,
                                                     java.lang.CharSequence seq)
        Given a CharSequence retrieved from a semantic value parsed from a child of the parse node supplied, return the start index. When the CharSequence is a StringToken, just return that start index. Otherwise, need to find the
      • findErrorsInRange

        public static ParseRange findErrorsInRange​(IParseNode pn,
                                                   int startIx,
                                                   int endIx)
        Utility method to find a single contiguous error inside of a specified region of a parse node. If there are multiple errors, the 'error region' inside of the specified startIx and endIx params is returned. This is used for identifying unparsed regions in building a formatting code model for this file to capture errors that exist between recognized parsed types. It's not OK to just treat them as whitespace since IntelliJ complains. This also returns any text in comments or an extra ";" if that's in the text body.
      • findClosestParseNode

        public static IParseNode findClosestParseNode​(IParseNode parent,
                                                      int offset)
        Given a parse node, returns either that parse node or a child of that parse node.
      • getParseNodeErrors

        public static java.util.List<ParseError> getParseNodeErrors​(IParseNode pnode,
                                                                    int max)
      • getInputString

        public static java.lang.String getInputString​( file,
                                                      int startIndex,
                                                      int i)
      • escapeObject

        public static java.lang.String escapeObject​(java.lang.Object value)
      • convertSemanticValue

        public static java.lang.Object convertSemanticValue​(java.lang.Class svClass,
                                                            java.lang.Object value)
      • clearSemanticValue

        public static void clearSemanticValue​(java.lang.Object value,
                                              IParseNode source)
      • firstCharFromValue

        public static int firstCharFromValue​(java.lang.Object value)
      • toSemanticString

        public static java.lang.CharSequence toSemanticString​(java.lang.Object value)
      • toSemanticStringLength

        public static int toSemanticStringLength​(java.lang.Object value)
      • toLength

        public static int toLength​(java.lang.Object val)
      • isArrayParselet

        public static boolean isArrayParselet​(Parselet parselet)
      • nodeToSemanticValue

        public static java.lang.Object nodeToSemanticValue​(java.lang.Object node)
        Use this method to convert the return value of the parse method into the semantic value produced. Usually the parse method returns an IParseNode.
      • initComponent

        public static java.lang.Object initComponent​(java.lang.Object obj)
      • startComponent

        public static java.lang.Object startComponent​(java.lang.Object obj)
      • validateComponent

        public static java.lang.Object validateComponent​(java.lang.Object obj)
      • realInitAndStartComponent

        public static java.lang.Object realInitAndStartComponent​(java.lang.Object obj)
        TODO: this just inits and starts the type for those cases where you can't do validate
      • initAndStartComponent

        public static java.lang.Object initAndStartComponent​(java.lang.Object obj)
        This really does init, start, and validate. TODO: Should fix this at some point.
      • stopComponent

        public static void stopComponent​(java.lang.Object obj)
      • getParseResult

        public static java.lang.Object getParseResult​(java.lang.Object node)
      • toLanguageString

        public static java.lang.String toLanguageString​(Parselet parselet,
                                                        ISemanticNode node)
      • restartComponent

        public static void restartComponent​(java.lang.Object obj)
      • reinitComponent

        public static void reinitComponent​(java.lang.Object obj)
      • styleString

        public static void styleString​(IStyleAdapter adapter,
                                       java.lang.Object semanticValue,
                                       IParseNode node,
                                       java.lang.String strVal,
                                       boolean escape)
      • styleString

        public static void styleString​(IStyleAdapter adapter,
                                       java.lang.Object semanticValue,
                                       java.lang.String styleName,
                                       java.lang.CharSequence strVal,
                                       boolean escape)
      • getStyleStart

        public static java.lang.CharSequence getStyleStart​(java.lang.String styleName)
      • getStyleEnd

        public static java.lang.CharSequence getStyleEnd()
      • isSpacingNode

        public static boolean isSpacingNode​(IParseNode childParseNode)
      • styleString

        public static void styleString​(IStyleAdapter adapter,
                                       java.lang.String styleName,
                                       java.lang.CharSequence strVal,
                                       boolean escape)
      • toStyledChild

        public static void toStyledChild​(IStyleAdapter adapter,
                                         ParentParseNode parent,
                                         java.lang.Object child,
                                         int childIx)
      • toStyledString

        public static void toStyledString​(IStyleAdapter adapter,
                                          java.lang.Object parseNode)
      • styleSemanticValue

        public static java.lang.CharSequence styleSemanticValue​(java.lang.Object semanticValue,
                                                                Parselet p)
      • styleSemanticValue

        public static java.lang.CharSequence styleSemanticValue​(java.lang.Object semanticValue,
                                                                java.lang.Object result)
      • styleParseResult

        public static java.lang.Object styleParseResult​(java.lang.String layerPath,
                                                        java.lang.String dispLayerPath,
                                                        java.lang.String fileName,
                                                        boolean displayErrors,
                                                        boolean isLayer,
                                                        java.lang.Object result,
                                                        boolean enabled)
      • styleSemanticValue

        public static java.lang.Object styleSemanticValue​(java.lang.Object semanticValue,
                                                          java.lang.Object result,
                                                          java.lang.String dispLayerPath,
                                                          java.lang.String fileName)
      • processComponent

        public static void processComponent​(ILifecycle comp)
      • getSemanticValueNestingDepth

        public static int getSemanticValueNestingDepth​(java.lang.Object sv)
      • resetSpacing

        public static java.lang.Object resetSpacing​(java.lang.Object parseNodeObj)
        Re-applies default spacing rules to the parse node given
      • resetSpacing

        public static void resetSpacing​(ISemanticNode node)
        Re-applies default spacing/new line rules to the parse node given. The spacing and newline objects have their parse nodes replaced by the generateParseNode
      • reformatParseNode

        public static void reformatParseNode​(IParseNode node)
        Removes the formatting parse-nodes, like SpacingParseNode and NewlineParseNode and replaces them with actual text based on the reformat algorithm. Some background on this: when we re-generate a model, i.e. update the parse-node representation for changes in the semantic node tree, we are unable to determine the spacing and other text which is generated based on the complete context. Instead, we insert these formatting parse-nodes as placeholders. The format process starts then from the top of the file and can accurately generate the indentation, newlines and spaces as per the formatting rules. This method performs that global operation but also replaces the formatting parse-nodes with the actual formatting characters - e.g. the whitespace, newlines, etc. so they can be more easily manipulated. Operations like reparsing and generating the IDE representation of the parse-nodes requires that the spacing is all evaluated. See also ParseUtil.resetSpacing which does the opposite - replacing the explicit spacing nodes with generated nodes so you can reformat a file.
      • resetStartIndexes

        public static void resetStartIndexes​(ISemanticNode node)
      • refreshParseNodeTree

        public static void refreshParseNodeTree​(ISemanticNode semNode)
      • equalSemanticValues

        public static boolean equalSemanticValues​(java.lang.Object o1,
                                                  java.lang.Object o2)
        Handles the case where there is a primitive boolean property. Each getX call on that property will return a new Boolean instance. During updateParseNode, we have a test to stop updating when we find a getX call that did not match from one to the next.
      • getPreSpacingForNode

        public static java.lang.String getPreSpacingForNode​(java.lang.Object parseNode)
        For retrieving the comments that precede a variable definition
      • getTrailingSpaceForNode

        public static java.lang.String getTrailingSpaceForNode​(java.lang.Object parseNode)
        For retrieving the comments that follow a variable definition on the same line
      • getSpacingForNode

        public static java.lang.String getSpacingForNode​(java.lang.Object parseNode)
      • getSpaceBefore

        public static ParseUtil.SpaceBeforeResult getSpaceBefore​(IParseNode parseNode,
                                                                 ISemanticNode value,
                                                                 Parselet spacing)
        Takes a parent child node and does a search through the parse-node tree looking for the semantic-value specified. Returns the comments (aka whitespace) right before that node, i.e. javadoc style comments
      • isCollapsibleNode

        public static boolean isCollapsibleNode​(java.lang.Object currentParent)
      • getLineNumberForNode

        public static int getLineNumberForNode​(IParseNode rootNode,
                                               IParseNode toFindPN)
      • countLinesInNode

        public static int countLinesInNode​(java.lang.CharSequence nodeStr)
      • countCodeLinesInNode

        public static int countCodeLinesInNode​(java.lang.CharSequence nodeStr)
      • countCodeLinesInNode

        public static int countCodeLinesInNode​(java.lang.CharSequence nodeStr,
                                               int len)
      • createSimpleParser

        public static LayeredSystem createSimpleParser​(java.lang.String classPath,
                                                       java.lang.String externalClassPath,
                                                       java.lang.String srcPath,
                                                       IExternalModelIndex modelIndex)
        (deprecated - for compatibility only) This version can potentially inherit a StrataCode index directory because it does not specify the mainDir yet may use the type index
      • createSimpleParser

        public static LayeredSystem createSimpleParser​(java.lang.String indexDir,
                                                       java.lang.String classPath,
                                                       java.lang.String externalClassPath,
                                                       java.lang.String srcPath,
                                                       IExternalModelIndex modelIndex)
        Creates a LayeredSystem from a single classPath, externalClassPath, and srcPath. You use this method when you do not have any layers but still want to leverage the low-level language processing capabilities of StrataCode. You can optionally provide an implementation of IExternalModelIndex to provide control over how models are cached and managed on the file system. This is useful to help synchronize the LayeredSystem's copy of a particular model with the version of the model managed by an external tool like an IDE.
      • reparse

        public static java.lang.Object reparse​(IParseNode pnode,
                                               java.lang.String newText)
      • serializeModel

        public static void serializeModel​(ISemanticNode node,
                                          java.lang.String serFileName,
                                          java.lang.String origFileName,
                                          Language lang)
      • deserializeModel

        public static ISemanticNode deserializeModel​(java.lang.String serFileName,
                                                     Language lang)
      • serializeParseNode

        public static void serializeParseNode​(IParseNode node,
                                              java.lang.String serFileName,
                                              java.lang.String origFileName,
                                              Language lang)
      • openParseNodeStream

        public static sc.binf.ParseInStream openParseNodeStream​(java.lang.String serFileName,
                                                                Language lang)