Package sc.parser

Class Parser

  • All Implemented Interfaces:
    java.lang.CharSequence, IString

    public class Parser
    extends java.lang.Object
    implements IString
    This is the parser class which holds state for a given parse of a file or buffer. Usually you access this class via Language. Parser maintains the input buffer and the location of the current parsed position in the input buffer. It also stores the current set of errors, parsing statistics, adn the current parselet.
    • Field Detail

      • semanticContext

        public SemanticContext semanticContext
        For languages like HTML which are not context free grammar, the parse nodes maintain additional information, like the current tag stack, used for matching the grammar
      • eof

        public boolean eof
      • currentErrorEndIndex

        public int currentErrorEndIndex
      • currentErrorStartIndex

        public int currentErrorStartIndex
      • currentErrors

        public java.util.List<ParseError> currentErrors
      • skippedErrors

        public java.util.List<ParseError> skippedErrors
        Nodes in the parse tree like ; and ) can be marked as errors and then skipped in the actual parse.
      • testedNodes

        public static int testedNodes
      • matchedNodes

        public static int matchedNodes
      • enablePartialValues

        public boolean enablePartialValues
      • restore

        public boolean restore
      • populateInst

        public java.lang.Object populateInst
        For some small grammars, we may want to populate the parent instance rather than create a new instance from the grammar
      • matchOnly

        public boolean matchOnly
    • Constructor Detail

      • Parser

        public Parser​(Language l,
                      char[] inputArray)
      • Parser

        public Parser​(Language l,
                      java.io.Reader reader,
                      int bufSize)
      • Parser

        public Parser​(Language l,
                      java.io.Reader reader)
    • Method Detail

      • charAt

        public char charAt​(int pos)
        Specified by:
        charAt in interface java.lang.CharSequence
        Specified by:
        charAt in interface IString
      • peekInputChar

        public char peekInputChar​(int pos)
      • getInputChar

        public char getInputChar​(int pos)
      • getInputString

        public java.lang.String getInputString​(int index,
                                               int len)
      • substring

        public IString substring​(int index,
                                 int endIndex)
        Specified by:
        substring in interface IString
      • startsWith

        public boolean startsWith​(java.lang.CharSequence other)
        Specified by:
        startsWith in interface IString
      • length

        public final int length()
        Specified by:
        length in interface java.lang.CharSequence
        Specified by:
        length in interface IString
      • parseStart

        public final java.lang.Object parseStart​(Parselet parselet)
      • reparseStart

        public final java.lang.Object reparseStart​(Parselet parselet,
                                                   java.lang.Object oldParseNode,
                                                   DiffContext dctx)
      • restoreStart

        public final java.lang.Object restoreStart​(Parselet parselet,
                                                   ISemanticNode oldModel,
                                                   sc.binf.ParseInStream pIn)
      • wrapErrors

        public final ParseError wrapErrors()
      • parseNext

        public final java.lang.Object parseNext​(Parselet parselet)
        This method is called by recognizers which are nested. it will do all of the work necessary before/after calling the recognizer.recognize method (i.e. rolling back changes if there are errors, dealing with the parse error returned - add it to the parser).
      • reparseNext

        public final java.lang.Object reparseNext​(Parselet parselet,
                                                  java.lang.Object oldParseNode,
                                                  DiffContext dctx,
                                                  boolean forceReparse,
                                                  Parselet exitParselet)
      • restoreNext

        public final java.lang.Object restoreNext​(Parselet parselet,
                                                  ISemanticNode oldModel,
                                                  RestoreCtx rctx,
                                                  boolean inherited)
        This method is called by recognizers which are nested. it will do all of the work necessary before/after calling the recognizer.recognize method (i.e. rolling back changes if there are errors, dealing with the parse error returned - add it to the parser).
      • changeCurrentIndex

        public final void changeCurrentIndex​(int ix)
        Called when we need to unwind the parser to an earlier or later state - TODO:performance - try changing the callers of this in the inner loop here to just set the field.
      • resetCurrentIndex

        public final java.lang.Object resetCurrentIndex​(int ix)
        Reset is used when we encounter an error and need to revert back to a previous index. Use restoreCurrentIndex when you are peeking into the stream, or need to move the semantic context backwards to reparse for errors
      • restoreCurrentIndex

        public final void restoreCurrentIndex​(int ix,
                                              java.lang.Object res)
        Like reset, but will save semantic context info. For example, if we peek ahead an HTML close tag, we remove it from the semantic context. When we restore the current index, we put the tag back again so we can match it again in the close tag.
      • getCurrentIndex

        public final int getCurrentIndex()
      • getLastStartIndex

        public final int getLastStartIndex()
        Returns the starting position for the active expression
      • parseError

        public final ParseError parseError​(java.lang.String errorCode)
      • isBetterError

        public static boolean isBetterError​(int currentStart,
                                            int currentEnd,
                                            int newStart,
                                            int newEnd,
                                            boolean replaceIfEqual)
      • parseError

        public ParseError parseError​(Parselet parselet,
                                     java.lang.Object partialValue,
                                     Parselet childParselet,
                                     java.lang.String errorCode,
                                     int start,
                                     int end,
                                     java.lang.Object... args)
      • addSkippedError

        public void addSkippedError​(ParseError skippedError)
        When it's convenient to parse a document being lenient on errors, skipped errors are enabled. You can parse a document even when it's missing a ; etc. Those errors are recorded and we move on.
      • parseResult

        public java.lang.Object parseResult​(Parselet parselet,
                                            java.lang.Object value,
                                            boolean skipSemanticValue)
      • getChars

        public void getChars​(int srcBegin,
                             int srcEnd,
                             char[] dst,
                             int dstBegin)
        Specified by:
        getChars in interface IString
      • toString

        public java.lang.String toString()
        Specified by:
        toString in interface java.lang.CharSequence
        Specified by:
        toString in interface IString
        Overrides:
        toString in class java.lang.Object
      • peekInputStr

        public int peekInputStr​(ArrString expectedValue,
                                boolean negated,
                                boolean ignoreCase)
        Warning: this method is tuned for performance so has a messy interface. Using ArrString because we want to use char arrays directly Look ahead into the currently parsed characters and match them against expectedValue. If !negated: return 0 for a match and 1 for a non-match, 2 for EOF If you pass in negated the match is opposite: return 1 for a match and 0 for a non-match, 0 for EOF
      • getStatInfo

        public static java.lang.String getStatInfo​(Parselet startParselet)
      • subSequence

        public java.lang.CharSequence subSequence​(int start,
                                                  int end)
        Specified by:
        subSequence in interface java.lang.CharSequence
      • atEOF

        public boolean atEOF()
      • getCacheStats

        public java.lang.String getCacheStats()
      • clearForSuccess

        public void clearForSuccess()