YylexTest.java

  1. package org.drip.sample.json;

  2. import java.io.*;

  3. import org.drip.json.parser.*;
  4. import org.drip.service.env.EnvManager;

  5. /*
  6.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  7.  */

  8. /*!
  9.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  14.  *
  15.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  16.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  17.  *      credit, commodity, equity, FX, and structured products.
  18.  *  
  19.  *      https://lakshmidrip.github.io/DROP/
  20.  *  
  21.  *  DROP is composed of three modules:
  22.  *  
  23.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  24.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  25.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  26.  *
  27.  *  DROP Analytics Core implements libraries for the following:
  28.  *  - Fixed Income Analytics
  29.  *  - Asset Backed Analytics
  30.  *  - XVA Analytics
  31.  *  - Exposure and Margin Analytics
  32.  *
  33.  *  DROP Portfolio Core implements libraries for the following:
  34.  *  - Asset Allocation Analytics
  35.  *  - Transaction Cost Analytics
  36.  *
  37.  *  DROP Numerical Core implements libraries for the following:
  38.  *  - Statistical Learning
  39.  *  - Numerical Optimizer
  40.  *  - Spline Builder
  41.  *  - Algorithm Support
  42.  *
  43.  *  Documentation for DROP is Spread Over:
  44.  *
  45.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  46.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  47.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  48.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  49.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  50.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  51.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  52.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  53.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  54.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  55.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  56.  *
  57.  *  Licensed under the Apache License, Version 2.0 (the "License");
  58.  *      you may not use this file except in compliance with the License.
  59.  *  
  60.  *  You may obtain a copy of the License at
  61.  *      http://www.apache.org/licenses/LICENSE-2.0
  62.  *  
  63.  *  Unless required by applicable law or agreed to in writing, software
  64.  *      distributed under the License is distributed on an "AS IS" BASIS,
  65.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  66.  *  
  67.  *  See the License for the specific language governing permissions and
  68.  *      limitations under the License.
  69.  */

  70. /**
  71.  * <i>YylexTest</i> is an Adaptation of the YylexTest Class from the RFC4627 compliant JSON Simple
  72.  * (https://code.google.com/p/json-simple/).
  73.  *  
  74.  * <br><br>
  75.  *  <ul>
  76.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  77.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AlgorithmSupportLibrary.md">Algorithm Support Library</a></li>
  78.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  79.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/json/README.md">JSON Serialization and De-serialization</a></li>
  80.  *  </ul>
  81.  * <br><br>
  82.  *
  83.  * @author Fang Yidong
  84.  * @author Lakshmi Krishnamurthy
  85.  */

  86. public class YylexTest {

  87.     public static final void testYylex() throws Exception{
  88.             String s="\"\\/\"";
  89.             System.out.println(s);
  90.             StringReader in = new StringReader(s);
  91.             Yylex lexer=new Yylex(in);
  92.             Yytoken token=lexer.yylex();
  93.             System.out.println (Yytoken.TYPE_VALUE == token.type);
  94.             System.out.println ("/".equalsIgnoreCase ((String) token.value));
  95.            
  96.             s="\"abc\\/\\r\\b\\n\\t\\f\\\\\"";
  97.             System.out.println(s);
  98.             in = new StringReader(s);
  99.             lexer=new Yylex(in);
  100.             token=lexer.yylex();
  101.             System.out.println (Yytoken.TYPE_VALUE == token.type);
  102.             System.out.println ("abc/\r\b\n\t\f\\".equalsIgnoreCase ((String)token.value));
  103.            
  104.             s="[\t \n\r\n{ \t \t\n\r}";
  105.             System.out.println(s);
  106.             in = new StringReader(s);
  107.             lexer=new Yylex(in);
  108.             token=lexer.yylex();
  109.             System.out.println (Yytoken.TYPE_LEFT_SQUARE == token.type);
  110.             token=lexer.yylex();
  111.             System.out.println (Yytoken.TYPE_LEFT_BRACE == token.type);
  112.             token=lexer.yylex();
  113.             System.out.println (Yytoken.TYPE_RIGHT_BRACE == token.type);
  114.            
  115.             s="\b\f{";
  116.             System.out.println(s);
  117.             in = new StringReader(s);
  118.             lexer=new Yylex(in);
  119.             ParseException err=null;
  120.             try{
  121.                     token=lexer.yylex();
  122.             }
  123.             catch(ParseException e){
  124.                     err=e;
  125.                     System.out.println("error:"+err);
  126.                     System.out.println (ParseException.ERROR_UNEXPECTED_CHAR == e.getErrorType());
  127.                     System.out.println (0 == e.getPosition());
  128.                     System.out.println (Character.toString ('\b') == e.getUnexpectedObject());
  129.             }
  130.             catch(IOException ie){
  131.                     throw ie;
  132.             }
  133.             System.out.println (err!=null);
  134.            
  135.             s="{a : b}";
  136.             System.out.println(s);
  137.             in = new StringReader(s);
  138.             lexer=new Yylex(in);
  139.             err=null;
  140.             try{
  141.                     lexer.yylex();
  142.                     token=lexer.yylex();
  143.             }
  144.             catch(ParseException e){
  145.                     err=e;
  146.                     System.out.println("error:"+err);
  147.                     System.out.println (ParseException.ERROR_UNEXPECTED_CHAR == e.getErrorType());
  148.                     System.out.println (Character.toString ('a') == e.getUnexpectedObject());
  149.                     System.out.println (1 == e.getPosition());
  150.             }
  151.             catch(IOException ie){
  152.                     throw ie;
  153.             }
  154.             System.out.println (err!=null);
  155.     }

  156.     public static final void main (
  157.         final String[] astrArgs)
  158.         throws Exception
  159.     {
  160.         EnvManager.InitEnv ("");

  161.         testYylex();

  162.         EnvManager.TerminateEnv();
  163.     }
  164. }