Skip to content

Commit

Permalink
1.0.4 major changes added writer made stuff lazy , and made a lot of …
Browse files Browse the repository at this point in the history
…implicit got rid of instance of
  • Loading branch information
rajatkb committed Jan 4, 2020
1 parent 9a6106a commit e8726dc
Show file tree
Hide file tree
Showing 21 changed files with 182 additions and 69 deletions.
Binary file modified JsonParser/.cache-main
Binary file not shown.
1 change: 1 addition & 0 deletions JsonParser/newfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"1":2,"3":[-1,2,-3.005640e+00,4.356560e+00],"hello":true}
52 changes: 41 additions & 11 deletions JsonParser/src/com/api/Jasp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,39 @@ import com.json.traits.JsonFactory
import com.json.traits.JsonMapTrait
import com.json.traits.JsonMapTrait
import com.json.traits.JsonFactory
import com.json.traits.JsonNumberTrait
import com.json.traits.JsonStringTrait
import com.json.traits.JsonBoolTrait
import com.json.traits.JsonMapTrait
import com.json.traits.JsonMapTrait
import com.json.traits.JsonListTrait
import com.json.traits.JsonListTrait
import com.json.traits.JsonBoolTrait
import com.json.traits.JsonNumberTrait
import com.json.traits.JsonStringTrait
import java.nio.channels.FileChannel
import java.util.RandomAccess
import java.io.RandomAccessFile
import java.nio.ByteBuffer
import com.json.traits.JsonWriteable
import java.io.BufferedOutputStream
import java.io.FileOutputStream

object Jasp {
private val defaultInstance = JsonPrototypeFactory.getInstance( new JsonObject(),new JsonArray(),new JsonNumber(0),new JsonString(null),new JsonBoolean(false))

private val defaultInstance = JsonPrototypeFactory.getInstance(new JsonObject(), new JsonArray(), new JsonNumber(0), new JsonString(null), new JsonBoolean(false))

// THE IMPLICIT ARE ALWAYS BOUND TO THE CURRENT FACTORY OBJECT THAT CREATES THE OBJECTS BASED ON USER IMPLEMENTATION
// OR WITH DEFAULT SUPPORTED BY THE LIBRARY
implicit def numtoValue(a: Double): JsonValue = JsonPrototypeFactory.getCurrentInstance().createJsonNumberEntity(a)
implicit def numtoKey(a: Double): JsonKey = JsonPrototypeFactory.getCurrentInstance().createJsonNumberEntity(a)
implicit def stringtoValue(a: String): JsonValue = JsonPrototypeFactory.getCurrentInstance().createJsonStringEntity(a)
implicit def stringtoKey(a: String): JsonKey = JsonPrototypeFactory.getCurrentInstance().createJsonStringEntity(a)
implicit def booltoKey(a: Boolean): JsonKey = JsonPrototypeFactory.getCurrentInstance().createJsonBooleanEntity(a)
implicit def booltoValue(a: Boolean): JsonValue = JsonPrototypeFactory.getCurrentInstance().createJsonBooleanEntity(a)
implicit def numtoKeyArrowAssoc(a: Double): ArrowAssoc[JsonKey] = new ArrowAssoc(a)
implicit def stringtoKeyArrowAssoc(a: String): ArrowAssoc[JsonKey] = new ArrowAssoc(a)
implicit def booleantoKeyArrowAssoc(a: Boolean): ArrowAssoc[JsonKey] = new ArrowAssoc(a)
implicit def num2Value(a: Double): JsonValue = JsonPrototypeFactory.getCurrentInstance().createJsonNumberEntity(a)
implicit def num2Key(a: Double): JsonKey = JsonPrototypeFactory.getCurrentInstance().createJsonNumberEntity(a)
implicit def string2Value(a: String): JsonValue = JsonPrototypeFactory.getCurrentInstance().createJsonStringEntity(a)
implicit def string2Key(a: String): JsonKey = JsonPrototypeFactory.getCurrentInstance().createJsonStringEntity(a)
implicit def bool2Key(a: Boolean): JsonKey = JsonPrototypeFactory.getCurrentInstance().createJsonBooleanEntity(a)
implicit def bool2Value(a: Boolean): JsonValue = JsonPrototypeFactory.getCurrentInstance().createJsonBooleanEntity(a)
implicit def num2KeyArrowAssoc(a: Double): ArrowAssoc[JsonKey] = new ArrowAssoc(a)
implicit def string2KeyArrowAssoc(a: String): ArrowAssoc[JsonKey] = new ArrowAssoc(a)
implicit def boolean2KeyArrowAssoc(a: Boolean): ArrowAssoc[JsonKey] = new ArrowAssoc(a)

object JSON {

Expand Down Expand Up @@ -114,6 +131,19 @@ object Jasp {
jsonBool))(fromFile)

}

def toFile(jsonObject: JsonWriteable, filename: String) = {
try {
val target = new BufferedOutputStream( new FileOutputStream(filename) );
jsonObject.getStringStream().foreach(s => target.write(s.getBytes) )
target.close()
} catch {
case e: FileNotFoundException =>
Logger.error("File $filename not found"); throw e
case e: IOException => Logger.error("Something went wrong when writing file"); throw e
}
}

}

}
Expand Down
4 changes: 3 additions & 1 deletion JsonParser/src/com/json/basic/JsonArray.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import com.json.traits.JsonWriteable
object JsonArray {
def apply(a1: JsonValue, a2: JsonValue*) = new JsonArray(a1 +: a2)
def apply(value: Seq[JsonValue] = Nil) = new JsonArray(value)
implicit def value2Array(a:JsonValue) = a match { case e:JsonArray => e;
case _ => throw new ClassCastException("Cannot cast "+a.getClass +" to "+this.getClass)}
}

class JsonArray(value: Seq[JsonValue] = Nil) extends JsonListTrait(value) {
override def toString() = "[" + value.mkString(",") + "]"
override def copy(v: Any) = new JsonArray(v.asInstanceOf[Seq[JsonValue]])
override def copy(v: Seq[JsonValue]) = new JsonArray(v)

def this(a1: JsonValue, a2: JsonValue*) = this(a1 +: a2)

Expand Down
5 changes: 4 additions & 1 deletion JsonParser/src/com/json/basic/JsonBoolean.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package com.json.basic

import com.json.traits.JsonBoolTrait
import com.json.traits.JsonWriteable
import com.json.traits.JsonValue

object JsonBoolean{
def apply(b:Boolean) = new JsonBoolean(b)
implicit def value2Boolean(a:JsonValue) = a match { case e:JsonBoolean => e;
case _ => throw new ClassCastException("Cannot cast "+a.getClass +" to "+this.getClass)}
}

class JsonBoolean(b:Boolean) extends JsonBoolTrait(b) {
override def toString() = b.toString()
override def copy(v:Any) = new JsonBoolean(v.asInstanceOf[Boolean])
override def copy(v:Boolean) = new JsonBoolean(v)

}

4 changes: 3 additions & 1 deletion JsonParser/src/com/json/basic/JsonNumber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import com.json.traits.JsonWriteable

object JsonNumber {
def apply(value: Double) = new JsonNumber(value)
implicit def value2Number(a:JsonValue) = a match { case e:JsonNumber => e;
case _ => throw new ClassCastException("Cannot cast "+a.getClass +" to "+this.getClass)}
}

case class JsonNumber(value: Double) extends JsonNumberTrait(value) {
override def toString() = value.toString()
override def copy(v: Any) = new JsonNumber(v.asInstanceOf[Double])
override def copy(v: Double) = new JsonNumber(v)

}

Expand Down
4 changes: 3 additions & 1 deletion JsonParser/src/com/json/basic/JsonObject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import com.json.traits.JsonWriteable
object JsonObject{
def apply(value:Map[JsonKey , JsonValue] = Map()) = new JsonObject(value)
def apply(args:(JsonKey,JsonValue)*) = new JsonObject(args.toMap)
implicit def value2Map(a:JsonValue) = a match { case e:JsonObject => e;
case _ => throw new ClassCastException("Cannot cast "+a.getClass +" to "+this.getClass)}
}

class JsonObject(value:Map[JsonKey , JsonValue] = Map()) extends JsonMapTrait(value) {

override def toString() = "{"+value.toList.map(f => f._1 + ":" + f._2).mkString(",\n")+"}"
override def copy(v:Any) = new JsonObject(v.asInstanceOf[Map[JsonKey,JsonValue]])
override def copy(v:Map[JsonKey , JsonValue]) = new JsonObject(v)


def this(args:(JsonKey,JsonValue)* ) = this(args.toMap)
Expand Down
7 changes: 4 additions & 3 deletions JsonParser/src/com/json/basic/JsonString.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.json.basic

import com.json.traits.JsonStringTrait
import com.json.traits.JsonWriteable
import com.json.traits.JsonValue

/**
* :JsonString => string
Expand All @@ -10,11 +11,11 @@ import com.json.traits.JsonWriteable

object JsonString{
def apply(value:String) = new JsonString(value)
implicit def value2String(a:JsonValue) = a match { case e:JsonString => e;
case _ => throw new ClassCastException("Cannot cast "+a.getClass +" to "+this.getClass)}
}

case class JsonString(value:String) extends JsonStringTrait(value) {
override def toString() = if(value == null) "undefined" else "\""+value+"\""
override def copy(v:Any) = new JsonString(v.asInstanceOf[String])


override def copy(v:String) = new JsonString(v)
}
10 changes: 7 additions & 3 deletions JsonParser/src/com/json/traits/JsonBoolTrait.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.json.traits

abstract class JsonBoolTrait(b:Boolean) extends JsonValue with JsonKey{
override def copy(a:Any):JsonBoolTrait
def copy(a:Boolean):JsonBoolTrait
override def apply(key:JsonKey):JsonValue = throw new IllegalAccessException("JsonBooleanTrait does not supports apply, try getValue()")
override def apply(key:Int):JsonValue = throw new IllegalAccessException("JsonBooleanTrait does not supports apply, try getValue()")
override def getValue() = b
override def getValue():Boolean = b

override def getStringStream() = Stream(b.toString())

}

object JsonBoolTrait {
implicit def value2Bool(a:JsonValue) = a match { case e:JsonBoolTrait => e;
case _ => throw new ClassCastException("Cannot cast "+a.getClass +" to "+this.getClass)}
}
21 changes: 16 additions & 5 deletions JsonParser/src/com/json/traits/JsonListTrait.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package com.json.traits

abstract class JsonListTrait(value:Seq[JsonValue]) extends JsonUnit with JsonValue {
override def copy(a:Any):JsonListTrait
def copy(a:Seq[JsonValue]):JsonListTrait

override def apply(key:Int):JsonValue = value.applyOrElse(key, null)
override def apply(key:JsonKey):JsonValue = throw new IllegalAccessException("JsonArrayTrait does not supports apply on JsonKey, try getValue()")
override def getValue() = value
override def apply(key:JsonKey):JsonValue = key match {
case key:JsonNumberTrait => value.applyOrElse(key.getValue().toInt, null)
case _ => throw new IllegalArgumentException("Non JsonNumber argument given ")
}

override def getValue():Seq[JsonValue] = value


override def getStringStream() = {
Stream("[")++value.toStream.flatMap(f => f.getStringStream() ++ Stream(","))++Stream("]")
(value.toStream.flatMap(f => Stream(",")++f.getStringStream())) match {
case Stream() => Stream("[")++Stream("]")
case v => Stream("[")++v.tail++Stream("]")
}
}

}

object JsonListTrait {
implicit def value2Array(a:JsonValue) = a match { case e:JsonListTrait => e;
case _ => throw new ClassCastException("Cannot cast "+a.getClass +" to "+this.getClass)}
}
25 changes: 21 additions & 4 deletions JsonParser/src/com/json/traits/JsonMapTrait.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@ package com.json.traits
//import scala.collection.mutable.HashMap

abstract class JsonMapTrait(value:Map[JsonKey , JsonValue]) extends JsonUnit with JsonValue {
override def copy(a:Any):JsonMapTrait

def copy(a:Map[JsonKey , JsonValue]):JsonMapTrait

override def apply(key:JsonKey) = value.get(key).getOrElse(null)
override def apply(int:Int) = throw new IllegalAccessException("JsonMapTrait does not supports apply on Int, try getValue()")
override def getValue():Map[JsonKey,JsonValue] = value

override def getValue():Map[JsonKey,JsonValue] = value

override def getStringStream() = {
Stream("{") ++ value.toStream.flatMap(f => f._1.getStringStream()++Stream(":")++f._2.getStringStream() ++ Stream(",")) ++ Stream("}")

(value.toStream.flatMap(f => {
val v = f._1 match { case e:JsonStringTrait => e.getStringStream();
case e => Stream("\"")++e.getStringStream()++Stream("\"") }

Stream(",")++ v ++ Stream(":")++f._2.getStringStream()}
)) match {
case Stream() => Stream("{") ++ Stream("}")
case v => Stream("{")++v.tail++Stream("}")
}

}



}


object JsonMapTrait {
implicit def value2Map(a:JsonValue) = a match { case e:JsonMapTrait => e;
case _ => throw new ClassCastException("Cannot cast "+a.getClass +" to "+this.getClass)}
}
20 changes: 15 additions & 5 deletions JsonParser/src/com/json/traits/JsonNumberTrait.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.json.traits

abstract class JsonNumberTrait(num:Double) extends JsonKey with JsonValue {
override def copy(a:Any):JsonNumberTrait
def copy(a:Double):JsonNumberTrait
override def apply(key:JsonKey):JsonValue = throw new IllegalAccessException("JsonNumberTrait does not supports apply, try getValue()")
override def apply(key:Int):JsonValue = throw new IllegalAccessException("JsonNumberTrait does not supports apply, try getValue()")
override def getValue() = num

override def getStringStream() = Stream(num.toString())
override def getValue():Double = num

override def getStringStream() = Stream( this.printDouble(num))

private def printDouble(a:Double) = {
if(a < Int.MaxValue )
if((a - a.toInt) != 0) f"$a%e" else a.toInt.toString()
else
f"$a%e"
}

}

object JsonNumberTrait {
implicit def value2Number(a:JsonValue) = a match { case e:JsonNumberTrait => e;
case _ => throw new ClassCastException("Cannot cast "+a.getClass +" to "+this.getClass)}
}
10 changes: 7 additions & 3 deletions JsonParser/src/com/json/traits/JsonStringTrait.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.json.traits

abstract class JsonStringTrait(str:String) extends JsonKey with JsonValue {
override def copy(a:Any):JsonStringTrait
override def getValue():String = str
def copy(a:String):JsonStringTrait
override def getValue():String = str
override def apply(key:JsonKey):JsonValue = throw new IllegalAccessException("JsonStringTrait does not supports apply, try getValue()")
override def apply(key:Int):JsonValue = throw new IllegalAccessException("JsonStringTrait does not supports apply, try getValue()")

override def getStringStream() = {
Stream("\""+str+"\"")
Expand All @@ -13,3 +12,8 @@ abstract class JsonStringTrait(str:String) extends JsonKey with JsonValue {

}

object JsonStringTrait {
implicit def value2String(a:JsonValue) = a match { case e:JsonStringTrait => e;
case _ => throw new ClassCastException("Cannot cast "+a.getClass +" to "+this.getClass)}
}

3 changes: 1 addition & 2 deletions JsonParser/src/com/json/traits/JsonUnit.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.json.traits

trait JsonUnit {
trait JsonUnit{
override def toString():String
def getValue():Any
def copy(a:Any):JsonUnit
}
1 change: 0 additions & 1 deletion JsonParser/src/com/json/traits/JsonValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ package com.json.traits

trait JsonValue extends JsonUnit with JsonWriteable {
def apply(key:JsonKey):JsonValue
def apply(key:Int):JsonValue
}
1 change: 1 addition & 0 deletions JsonParser/src/com/lexer/analyzer/LexemeGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class LexemeGenerator(tokens: => Stream[TextToken]) extends LexemeGeneratorTrait


// Check for number
case (`stateS`,v) if (v == '-') => (state3,false,buffer.append(v), prevlexArray)
case (`stateS`,v) if (v.isDigit) => (state3,false,buffer.append(v),prevlexArray)
case (`state3`,v) if (v.isDigit) => (state3,false,buffer.append(v),prevlexArray)
case (`state3`,v) if (v == '.') => (state4,false,buffer.append(v),prevlexArray)
Expand Down
10 changes: 5 additions & 5 deletions JsonParser/src/com/lexer/lexicon/BooleanLexeme.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import com.lexer.traits.SymbolTable
import com.logger.Logger

class BooleanLexeme(b:String, lineNumber:Int, columnNumber:Int) extends Lexeme with SymbolTable {
def getValue():Boolean = try { b.toBoolean }
catch{
case e:Exception => Logger.error("Something wrong with Lexeme have bad string data for boolean "+b)
throw e
}
def getValue():Boolean = try { b.toBoolean }
catch{
case e:Exception => Logger.error("Something wrong with Lexeme have bad string data for boolean "+b)
throw e
}
def getSymbol() = this.BOOL
def getLineNumber():Int = lineNumber
def getColumnNumber():Int = columnNumber
Expand Down
2 changes: 1 addition & 1 deletion JsonParser/src/com/parser/director/ParseTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ trait ParseTable extends SymbolTable {

case (stackSymb,lex) if (stackSymb == lex) => stack.tail
case _ =>try{
throw new IllegalStateException("expected "+stack.head+"found '"+lexeme.getValue()+"' cannot parse Json, illegal symbol at (l,c):"+lexeme.getLineNumber()+":"+lexeme.getColumnNumber())
throw new IllegalStateException("Look ahead or action for "+stack.head+" not found on '"+lexeme+"' cannot parse Json, illegal symbol at (l,c):"+lexeme.getLineNumber()+":"+lexeme.getColumnNumber())
}catch{
case e:IllegalAccessError => throw new IllegalStateException("found '"+lexeme.getSymbol()+"' cannot parse Json, illegal symbol at (l,c):"+lexeme.getLineNumber()+":"+lexeme.getColumnNumber()+"\n"+e)
}
Expand Down
4 changes: 1 addition & 3 deletions JsonParser/src/com/parser/director/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import com.file.tokenizer.Tokenizer
import com.lexer.traits.LexemeGeneratorTrait

class Parser(lexer:LexemeGeneratorTrait, builder:JsonBuilderTrait) extends ParseTable{




def parse() = {
val stack = lexer.getStream().foldLeft(List(this.S,this.STOP))((stack,lexeme) => {
this.stackOperation(stack, lexeme , builder)
Expand Down
Loading

0 comments on commit e8726dc

Please sign in to comment.