Parses the input string into a grammar.

@param inputStr the string to parse.
@return the grammar.
510003
public static RePairGrammar parse(String inputStr) {

    Date start0 = new Date();
    LOGGER.debug("input string (" + String.valueOf(countSpaces(inputStr) + 1) + " tokens) ");

    RePairGrammar grammar = new RePairGrammar();

    // two data structures
    //
    // 1.0. - the string
    ArrayList<RePairSymbolRecord> symbolizedString = new ArrayList<RePairSymbolRecord>(
        countSpaces(inputStr) + 1);

    // 2.0. - the priority queue
    RepairPriorityQueue digramsQueue = new RepairPriorityQueue();

    // 3.0. - the R0 digrams occurrence hashtable: <digram string> -> <R0 occurrence indexes>
    HashMap<String, ArrayList<Integer>> digramsTable = new HashMap<String, ArrayList<Integer>>();

    // 3.1. - all digrams ever seen will be here
    // HashMap<String, ArrayList<Integer>> allDigramsTable = new HashMap<String,
    // ArrayList<Integer>>();

    // tokenize the input string
    StringTokenizer st = new StringTokenizer(inputStr, " ");

    // while there are tokens, populate digrams hash and construct the table
    //
    int stringPositionCounter = 0;
    while (st.hasMoreTokens()) {

      // got token, make a symbol
      String token = st.nextToken();
      RePairSymbol symbol = new RePairSymbol(token, stringPositionCounter);
      // LOGGER.debug("token @" + stringPositionCounter + ": " + token);

      // add it to the string
      RePairSymbolRecord sr = new RePairSymbolRecord(symbol);
      symbolizedString.add(sr);

      // make a digram if we at the second and all consecutive places
      if (stringPositionCounter > 0) {

        // digram str
        StringBuffer digramStr = new StringBuffer();
        digramStr.append(symbolizedString.get(stringPositionCounter - 1).getPayload().toString())
            .append(SPACE)
            .append(symbolizedString.get(stringPositionCounter).getPayload().toString());

        // fill the digram occurrence frequency
        if (digramsTable.containsKey(digramStr.toString())) {
          digramsTable.get(digramStr.toString()).add(stringPositionCounter - 1);
          // LOGGER.debug(" .added a digram entry to: " + digramStr.toString());
        }
        else {
          ArrayList<Integer> arr = new ArrayList<Integer>();
          arr.add(stringPositionCounter - 1);
          digramsTable.put(digramStr.toString(), arr);
          // LOGGER.debug(" .created a digram entry for: " + digramStr.toString());
        }

        symbolizedString.get(stringPositionCounter - 1).setNext(sr);
        sr.setPrevious(symbolizedString.get(stringPositionCounter - 1));

      }

      // go on
      stringPositionCounter++;
    }
    Date start1 = new Date();
    LOGGER.debug("tokenized input and extracted all pairs in "
        + SAXProcessor.timeToString(start0.getTime(), start1.getTime()) + ", " + digramsTable.size()
        + " distinct pairs found");
    // LOGGER.debug("parsed the input string into the doubly linked list of tokens ...");
    // LOGGER.debug("RePair input: " + asString(symbolizedString));
    // LOGGER.debug("digrams table: " + printHash(digramsTable).replace("\n",
    // "\n "));
    // allDigramsTable.putAll(digramsTable);

    // LOGGER.debug("populating the priority queue...");
    // populate the priority queue and the index -> digram record map
    //
    for (Entry<String, ArrayList<Integer>> e : digramsTable.entrySet()) {
      if (e.getValue().size() > 1) {
        // create a digram record
        RepairDigramRecord dr = new RepairDigramRecord(e.getKey(), e.getValue().size());
        // put the record into the priority queue
        digramsQueue.enqueue(dr);
      }
    }
    Date start2 = new Date();
//    LOGGER.debug("built the priority queue in "
//        + SAXProcessor.timeToString(start1.getTime(), start2.getTime()) + ", " + digramsQueue.size()
//        + " digrams in the queue");
    // LOGGER.debug(digramsQueue.toString().replace("\n",
    // "\n "));
    // System.out.println(digramsQueue.toString());

    // start the Re-Pair cycle
    //
    RepairDigramRecord entry = null;
    while ((entry = digramsQueue.dequeue()) != null) {

      // LOGGER.debug(" *the current R0: " + asString(symbolizedString));
      // LOGGER.debug(" *digrams table: " + printHash(digramsTable).replace("\n",
      // "\n "));
      //
      // LOGGER.debug(" *polled a priority queue entry: " + entry.str + " : " + entry.freq);
      // LOGGER.debug(" *" + digramsQueue.toString().replace("\n",
      // "\n "));
      // digramsQueue.runCheck();

      // create a new rule
      //
      ArrayList<Integer> occurrences = digramsTable.get(entry.str);

      RePairSymbolRecord first = symbolizedString.get(occurrences.get(0));
      RePairSymbolRecord second = first.getNext();

      RePairRule r = new RePairRule(grammar);
      // System.out.println("polled a priority queue entry: " + entry.str + " : " + entry.freq + " -> created the rule " + r.ruleNumber);

      r.setFirst(first.getPayload());
      r.setSecond(second.getPayload());
      r.assignLevel();
      r.setExpandedRule(
          first.getPayload().toExpandedString() + SPACE + second.getPayload().toExpandedString());

      // LOGGER.debug(" .creating the rule: " + r.toInfoString());
      //
      // // substitute each digram entry with the rule
      // //
      // LOGGER.debug(" .substituting the digram at locations: " + occurrences.toString());
      HashSet<String> newDigrams = new HashSet<String>(occurrences.size());

      // sometimes we remove some of those...
      ArrayList<Integer> loopOccurrences = new ArrayList<Integer>(occurrences.size());
      for (Integer i : occurrences) {
        loopOccurrences.add(i);
      }
      while (!(loopOccurrences.isEmpty())) {

        // secure the position
        //
        int currentIndex = loopOccurrences.remove(0);
        RePairSymbolRecord currentS = symbolizedString.get(currentIndex);
        RePairSymbolRecord nextS = symbolizedString.get(currentIndex).getNext();

        // 1.0. create a new guard to replace the digram
        //
        RePairGuard g = new RePairGuard(r);
        g.setStringPosition(currentS.getIndex());
        r.addOccurrence(currentS.getIndex());
        RePairSymbolRecord guard = new RePairSymbolRecord(g);
        symbolizedString.set(currentIndex, guard);
        // also place a NULL placeholder next
        RePairSymbolRecord nextNotNull = nextS.getNext();
        guard.setNext(nextNotNull);
        if (null != nextNotNull) {
          nextNotNull.setPrevious(guard);
        }
        RePairSymbolRecord prevNotNull = currentS.getPrevious();
        guard.setPrevious(prevNotNull);
        if (null != prevNotNull) {
          prevNotNull.setNext(guard);
        }

        // 2.0 correct entry at the left
        //
        if (currentIndex > 0 && null != prevNotNull) {

          // cleanup old left digram
          String oldLeftDigram = prevNotNull.getPayload().toString() + " "
              + currentS.getPayload().toString();
          int newFreq = digramsTable.get(oldLeftDigram).size() - 1;
          // consoleLogger
          // .debug(" .removed left digram entry @" + prevNotNull.getPayload().getStringPosition()
          // + " " + oldLeftDigram + ", new freq: " + newFreq);
          digramsTable.get(oldLeftDigram).remove(Integer.valueOf(prevNotNull.getIndex()));
          if (oldLeftDigram.equalsIgnoreCase(entry.str)) {
            loopOccurrences.remove(Integer.valueOf(prevNotNull.getIndex()));
          }
          digramsQueue.updateDigramFrequency(oldLeftDigram, newFreq);

          // if it was the last entry...
          if (0 == newFreq) {
            digramsTable.remove(oldLeftDigram);
            newDigrams.remove(oldLeftDigram);
          }

          // and place the new digram entry
          String newLeftDigram = prevNotNull.getPayload().toString() + " " + r.toString();
          // see the new freq..
          if (digramsTable.containsKey(newLeftDigram)) {
            digramsTable.get(newLeftDigram).add(prevNotNull.getPayload().getStringPosition());
            // LOGGER.debug(" .added a digram entry to: " + newLeftDigram + ", @"
            // + prevNotNull.getPayload().getStringPosition());
          }
          else {
            ArrayList<Integer> arr = new ArrayList<Integer>();
            arr.add(prevNotNull.getPayload().getStringPosition());
            digramsTable.put(newLeftDigram, arr);
            // LOGGER.debug(" .created a digram entry for: " + newLeftDigram.toString()
            // + ", @" + prevNotNull.getPayload().getStringPosition());
          }
          newDigrams.add(newLeftDigram);

        }

        // 3.0 correct entry at the right
        //
        RePairSymbolRecord nextSS = nextS.getNext();
        if (currentIndex < symbolizedString.size() - 2 && null != nextSS) {

          // cleanup old left digram
          String oldRightDigram = nextS.getPayload().toString() + " "
              + nextSS.getPayload().toString();
          int newFreq = digramsTable.get(oldRightDigram).size() - 1;
          // consoleLogger
          // .debug(" .removed right digram entry @" + nextSS.getPayload().getStringPosition()
          // + " " + oldRightDigram + ", new freq: " + newFreq);
          digramsTable.get(oldRightDigram).remove(Integer.valueOf(nextS.getIndex()));
          if (oldRightDigram.equalsIgnoreCase(entry.str)) {
            loopOccurrences.remove(Integer.valueOf(nextS.getIndex()));
          }
          digramsQueue.updateDigramFrequency(oldRightDigram, newFreq);

          // if it was the last entry...
          if (0 == newFreq) {
            digramsTable.remove(oldRightDigram);
            newDigrams.remove(oldRightDigram);
          }

          // and place the new digram entry
          String newRightDigram = r.toString() + " " + nextSS.getPayload().toString();
          // see the new freq..
          if (digramsTable.containsKey(newRightDigram)) {
            digramsTable.get(newRightDigram).add(currentS.getPayload().getStringPosition());
            // LOGGER.debug(" .added a digram entry to: " + newRightDigram + ", @"
            // + currentS.getPayload().getStringPosition());
          }
          else {
            ArrayList<Integer> arr = new ArrayList<Integer>();
            arr.add(currentS.getPayload().getStringPosition());
            digramsTable.put(newRightDigram, arr);
            // LOGGER.debug(" .created a digram entry for: " + newRightDigram.toString()
            // + ", @" + currentS.getPayload().getStringPosition());
          }
          newDigrams.add(newRightDigram);

        }

      } // walk over all occurrences

      // voila -- remove the digram itself from the tracking table
      digramsTable.remove(entry.str);

      // update new digram frequencies and if needed place those into priority queue
      //
      for (String digramStr : newDigrams) {
        if (digramsTable.get(digramStr).size() > 1) {
          if (digramsQueue.containsDigram(digramStr)) {
            digramsQueue.updateDigramFrequency(digramStr, digramsTable.get(digramStr).size());
          }
          else {
            digramsQueue
                .enqueue(new RepairDigramRecord(digramStr, digramsTable.get(digramStr).size()));
          }
        }
      }

    }

    Date start3 = new Date();
    LOGGER.debug("finished repair grammar construction in "
        + SAXProcessor.timeToString(start2.getTime(), start3.getTime()));

    // LOGGER.debug("finished RePair run ...");
    // LOGGER.debug("R0: " + asString(symbolizedString));
    // LOGGER.debug("digrams table: " + printHash(digramsTable).replace("\n",
    // "\n "));
    // LOGGER.debug("digrams queue: " + digramsQueue.toString().replace("\n",
    // "\n "));

    grammar.setR0String(asString(symbolizedString));
    // and since all completed, set the expanded string too
    grammar.setR0ExpnadedString(inputStr.substring(0));

    return grammar;

  }
361914
@SuppressWarnings("resource")
    @Override
    public void parse(InputSource input) throws SAXException
    {
        mScanner = null;
        String sysIdStr = input.getSystemId();
        ReaderConfig cfg = mConfig;
        URL srcUrl = null;

        // Let's figure out input, first, before sending start-doc event
        InputStream is = null;
        Reader r = input.getCharacterStream();
        if (r == null) {
            is = input.getByteStream();
            if (is == null) {
                if (sysIdStr == null) {
                    throw new SAXException("Invalid InputSource passed: neither character or byte stream passed, nor system id specified");
                }
                try {
                    srcUrl = URLUtil.urlFromSystemId(sysIdStr);
                    is = URLUtil.inputStreamFromURL(srcUrl);
                } catch (IOException ioe) {
                    SAXException saxe = new SAXException(ioe);
                    ExceptionUtil.setInitCause(saxe, ioe);
                    throw saxe;
                }
            }
        }

        if (mContentHandler != null) {
            mContentHandler.setDocumentLocator(this);
            mContentHandler.startDocument();
        }

        /* Note: since we are reusing the same config instance, need to
         * make sure state is not carried forward. Thus:
         */
        cfg.resetState();

        try {
            String inputEnc = input.getEncoding();
            String publicId = input.getPublicId();

            // Got an InputStream and encoding? Can create a Reader:
            if (r == null && (inputEnc != null && inputEnc.length() > 0)) {
                r = DefaultInputResolver.constructOptimizedReader(cfg, is, false, inputEnc);
            }
            InputBootstrapper bs;
            SystemId systemId = SystemId.construct(sysIdStr, srcUrl);
            if (r != null) {
                bs = ReaderBootstrapper.getInstance(publicId, systemId, r, inputEnc);
                // false -> not for event reader; false -> no auto-closing
                mScanner = (BasicStreamReader) mStaxFactory.createSR(cfg, systemId, bs, false, false);
            } else {
                bs = StreamBootstrapper.getInstance(publicId, systemId, is);
                mScanner = (BasicStreamReader) mStaxFactory.createSR(cfg, systemId, bs, false, false);
            }

            // Need to get xml declaration stuff out now:
            {
                String enc2 = mScanner.getEncoding();
                if (enc2 == null) {
                    enc2 = mScanner.getCharacterEncodingScheme();
                }
                mEncoding = enc2;
            }
            mXmlVersion = mScanner.getVersion();
            mStandalone = mScanner.standaloneSet();
            mAttrCollector = mScanner.getAttributeCollector();
            mElemStack = mScanner.getInputElementStack();
            fireEvents();
        } catch (IOException io) {
            throwSaxException(io);
        } catch (XMLStreamException strex) {
            throwSaxException(strex);
        } finally {
            if (mContentHandler != null) {
                mContentHandler.endDocument();
            }
            // Could try holding onto the buffers, too... but
            // maybe it's better to allow them to be reclaimed, if
            // needed by GC
            if (mScanner != null) {
                BasicStreamReader sr = mScanner;
                mScanner = null;
                try {
                    sr.close();
                } catch (XMLStreamException sex) { }
            }
            if (r != null) {
                try {
                    r.close();
                } catch (IOException ioe) { }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ioe) { }
            }
        }
    }
1011221
public static MessageFilterParser createParser(String input) {
    	    ANTLRStringStream inputStream = new ANTLRStringStream(input);
    		MessageFilterLexer lexer = new MessageFilterLexer(inputStream);
    		CommonTokenStream tokens = new CommonTokenStream(lexer); 
    		
    		return new MessageFilterParser(tokens);
        }
1178169
private static InputSource stringToSource(String str) {
        InputSource is = new InputSource(new StringReader(str));
        is.setEncoding("UTF-8");
        return is;
    }
1094053
public Object parse(InputStream inputStream, String charset) {
        if (inputStream == null) {
            throw new IllegalArgumentException("inputStream must not be null");
        }
        if (charset == null) {
            throw new IllegalArgumentException("charset must not be null");
        }

        Object content;
        content = createParser().parse(inputStream, charset);
        return content;
    }
304448
public static Maybe<Pair<Node<PluralType>, CharSequence>> parse(String input) {
    return P_RULE.parse(input);
  }
1223106
public static Date parseDate(String dateStr, String pattern) {
        try {
            SimpleDateFormat format = null;
            if (StringUtils.isBlank(dateStr))
                return null;
            if (StringUtils.isNotBlank(pattern)) {
                format = new SimpleDateFormat(pattern);
                return format.parse(dateStr);
            }
            return parseDate(dateStr);
        } catch (Exception e) {
            throw new RuntimeException("无法解析日期字符[" + dateStr + "]");
        }
    }
353854
public XmlWrapperParser createParser(InputStream inputStream) {
        try {
			return new XmlWrapperParser(this, inputStream, getSupportedFormat());
		} catch (Exception e) {
			e.printStackTrace();
			throw new KriptonRuntimeException(e);
		}
	}
1191939
public Document parseString(String text) throws SAXException {
        try {
            return parse(new InputSource(new StringReader(text)));
        } catch (IOException e) {
            throw new RuntimeException("unexpected world exception while reading memory stream", e);
        }
    }
1010223
public static Expression parse(String input) throws ParseException {
        return new Parser(new StringReader(input), new Scope()).parse();
    }
1132325
public void parse(String str) {
		if (buffer.length() > 0) {
			str = buffer.toString().concat(str);
			buffer = new StringBuilder();
		}
		Reader reader = new StringReader(str);
		try {
			try {
				parse(reader);
			} finally {
				reader.close();
			}
		} catch (IOException ex) {
			/* ignore */
		}
	}
406768
@Override
	public ImmutableList<TargetAtom> parse(String input) throws TargetQueryParserException {
		StringBuffer bf = new StringBuffer(input.trim());
		if (!bf.substring(bf.length() - 2, bf.length()).equals(" .")) {
			bf.insert(bf.length() - 1, ' ');
		}
		if (!prefixes.isEmpty()) {
			// Update the input by appending the directives
			appendDirectives(bf);
		}		
		try {
			CharStream inputStream = CharStreams.fromString(bf.toString());
			TurtleOBDALexer lexer = new TurtleOBDALexer(inputStream);

			//substitute the standard ConsoleErrorListener (simply print out the error) with ThrowingErrorListener
            lexer.removeErrorListeners();
			lexer.addErrorListener(ThrowingErrorListener.INSTANCE);

			CommonTokenStream tokenStream = new CommonTokenStream(lexer);
			TurtleOBDAParser parser = new TurtleOBDAParser(tokenStream);
            //substitute the standard ConsoleErrorListener (simply print out the error) with ThrowingErrorListener
			parser.removeErrorListeners();
			parser.addErrorListener(ThrowingErrorListener.INSTANCE);

			return (ImmutableList<TargetAtom>)visitor.visitParse(parser.parse());
		} catch (RuntimeException e) {
			throw new TargetQueryParserException(e.getMessage(), e);
		}
	}
388902
public static Document getDomFromString( String str )
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware( true );

        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse( new InputSource( new StringReader( str ) ) );
        } catch ( Exception e ) {
            throw new CStorageException( "Error parsing the Xml response: " + str, e );
        }
    }
395265
public Map<String,String> parse(final String str, char separator) {
		if (str == null) {
			return new HashMap<String,String>();
		}
		return parse(str.toCharArray(), separator);
	}
602431
static Type parse(String str) {
        try {
            return doParse(str);
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
537739
public static ReadablePeriod parsePeriodString(final String periodStr) {
    final ReadablePeriod period;
    final char periodUnit = periodStr.charAt(periodStr.length() - 1);
    if (periodStr.equals("null") || periodUnit == 'n') {
      return null;
    }

    final int periodInt =
        Integer.parseInt(periodStr.substring(0, periodStr.length() - 1));
    switch (periodUnit) {
      case 'y':
        period = Years.years(periodInt);
        break;
      case 'M':
        period = Months.months(periodInt);
        break;
      case 'w':
        period = Weeks.weeks(periodInt);
        break;
      case 'd':
        period = Days.days(periodInt);
        break;
      case 'h':
        period = Hours.hours(periodInt);
        break;
      case 'm':
        period = Minutes.minutes(periodInt);
        break;
      case 's':
        period = Seconds.seconds(periodInt);
        break;
      default:
        throw new IllegalArgumentException("Invalid schedule period unit '"
            + periodUnit);
    }

    return period;
  }
1158415
public static LiteralType parse(String input) {
		LiteralType result=new LiteralType();
		parse(result, input);
		
		return result;
	}