Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading JSON IO tests to the new API, and removing the deprecated API #2104

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
94dd242
removed 8 year old deprecated functions from JSON IO and switched the…
jurgenvinju Dec 18, 2024
8c04832
squashing bugs in testing the JSON IO functions
jurgenvinju Dec 18, 2024
91fcd88
fixed the datetime NYIs and bugs
jurgenvinju Dec 18, 2024
e5d3b2a
fixed bugs with tuples and nodes
jurgenvinju Dec 18, 2024
0f2e799
improving tests and fixing bugs
jurgenvinju Dec 19, 2024
67329e2
added empty tuple to syntax without adding a type syntax for the empt…
jurgenvinju Dec 19, 2024
172e9cf
removed strange duplicated
jurgenvinju Dec 19, 2024
2a7c533
also added empty tuple to Pattern syntax
jurgenvinju Dec 19, 2024
349d4ee
more fixes. not there yet
jurgenvinju Dec 20, 2024
b1b59cc
normalized datetime output for testing purposes
jurgenvinju Dec 23, 2024
6e230e2
added normalization of rationals
jurgenvinju Dec 23, 2024
e3b3065
more progress on getting the tests right
jurgenvinju Dec 23, 2024
322e830
more bug squashing
jurgenvinju Dec 23, 2024
9fb3493
testing ignores orders in lists when testing value and node categories
jurgenvinju Dec 23, 2024
69dfc60
added docs and fixed the last bug
jurgenvinju Dec 24, 2024
437c892
comment
jurgenvinju Dec 24, 2024
083d129
added missing positional parameters to normalization
jurgenvinju Dec 24, 2024
3fb9b23
fixed compilation errors due to changes in AST API
jurgenvinju Dec 24, 2024
ab9b274
removed deprecated labels from elementAt and reducer functions
jurgenvinju Dec 24, 2024
8c4e2a7
fixed some weird layout
jurgenvinju Dec 24, 2024
4bbed9f
Update pom.xml
jurgenvinju Dec 24, 2024
ae54d3e
trying to cover more branches
jurgenvinju Dec 26, 2024
dea3dd5
increasing JSON IO tests branch and line coverage
jurgenvinju Dec 26, 2024
8dba998
fixed corner cases with very large numbers
jurgenvinju Dec 26, 2024
6af770b
fixed reals
jurgenvinju Dec 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<lucence-version>7.5.0</lucence-version>
<maven-version>3.9.8</maven-version>
<exec.mainClass>org.rascalmpl.shell.RascalShell</exec.mainClass>
<rascal.test.memory>2</rascal.test.memory>
<rascal.test.memory>3</rascal.test.memory>
<maven.compiler.release>11</maven.compiler.release>
<rascal-maven.version>0.28.9-BOOT1</rascal-maven.version>
</properties>
Expand Down
29 changes: 11 additions & 18 deletions src/org/rascalmpl/ast/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ public boolean hasArguments() {
public java.util.List<org.rascalmpl.ast.Expression> getArguments() {
throw new UnsupportedOperationException();
}
public boolean hasElements() {
return false;
}

public java.util.List<org.rascalmpl.ast.Expression> getElements() {
throw new UnsupportedOperationException();
}
public boolean hasElements0() {
return false;
}
Expand Down Expand Up @@ -6395,15 +6388,15 @@ public boolean isTuple() {
}

static public class Tuple extends Expression {
// Production: sig("Tuple",[arg("java.util.List\<org.rascalmpl.ast.Expression\>","elements")],breakable=false)
// Production: sig("Tuple",[arg("java.util.List\<org.rascalmpl.ast.Expression\>","elements0")],breakable=false)


private final java.util.List<org.rascalmpl.ast.Expression> elements;
private final java.util.List<org.rascalmpl.ast.Expression> elements0;

public Tuple(ISourceLocation src, IConstructor node , java.util.List<org.rascalmpl.ast.Expression> elements) {
public Tuple(ISourceLocation src, IConstructor node , java.util.List<org.rascalmpl.ast.Expression> elements0) {
super(src, node);

this.elements = elements;
this.elements0 = elements0;
}

@Override
Expand All @@ -6423,7 +6416,7 @@ protected void addForLineNumber(int $line, java.util.List<AbstractAST> $result)
}
ISourceLocation $l;

for (AbstractAST $elem : elements) {
for (AbstractAST $elem : elements0) {
$l = $elem.getLocation();
if ($l.hasLineColumn() && $l.getBeginLine() <= $line && $l.getEndLine() >= $line) {
$elem.addForLineNumber($line, $result);
Expand All @@ -6441,28 +6434,28 @@ public boolean equals(Object o) {
return false;
}
Tuple tmp = (Tuple) o;
return true && tmp.elements.equals(this.elements) ;
return true && tmp.elements0.equals(this.elements0) ;
}

@Override
public int hashCode() {
return 599 + 863 * elements.hashCode() ;
return 599 + 863 * elements0.hashCode() ;
}


@Override
public java.util.List<org.rascalmpl.ast.Expression> getElements() {
return this.elements;
public java.util.List<org.rascalmpl.ast.Expression> getElements0() {
return this.elements0;
}

@Override
public boolean hasElements() {
public boolean hasElements0() {
return true;
}

@Override
public Object clone() {
return newInstance(getClass(), src, (IConstructor) null , clone(elements));
return newInstance(getClass(), src, (IConstructor) null , clone(elements0));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public IConstructor visitExpressionCallOrTree(CallOrTree x) {
if (name.equals("seq")) {
IList list = vf.list();
Expression.List arg = (List) x.getArguments().get(0);
for (Expression y: arg.getElements()) {
for (Expression y: arg.getElements0()) {
list = list.append(y.accept(this));
}
return vf.constructor(RascalValueFactory.Symbol_Seq, list);
Expand All @@ -124,7 +124,7 @@ public IConstructor visitExpressionCallOrTree(CallOrTree x) {
if (name.equals("alt")) {
ISet set = vf.set();
Expression.Set arg = (Set) x.getArguments().get(0);
for(Expression y: arg.getElements()){
for(Expression y: arg.getElements0()){
set = set.insert(y.accept(this));
}
return vf.constructor(RascalValueFactory.Symbol_Alt, set);
Expand All @@ -134,7 +134,7 @@ public IConstructor visitExpressionCallOrTree(CallOrTree x) {
java.util.List<Expression> args = x.getArguments();
IConstructor head = args.get(0).accept(this);
IList rest = vf.list();
for (Expression arg: ((Expression.List)args.get(1)).getElements()) {
for (Expression arg: ((Expression.List)args.get(1)).getElements0()) {
rest = rest.append(arg.accept(this));
}
return vf.constructor(RascalValueFactory.Symbol_Tuple, head, rest);
Expand Down Expand Up @@ -171,7 +171,7 @@ public IConstructor visitExpressionCallOrTree(CallOrTree x) {
IConstructor arg = x.getArguments().get(0).accept(this);
Expression.List args = (Expression.List) x.getArguments().get(1);
IList seps = vf.list();
for (Expression elem: args.getElements()) {
for (Expression elem: args.getElements0()) {
seps = seps.append(elem.accept(this));
}
return vf.constructor(RascalValueFactory.Symbol_IterStarSeps, arg, seps);
Expand All @@ -181,7 +181,7 @@ public IConstructor visitExpressionCallOrTree(CallOrTree x) {
IConstructor arg = x.getArguments().get(0).accept(this);
Expression.List args = (Expression.List) x.getArguments().get(1);
IList seps = vf.list();
for (Expression elem: args.getElements()) {
for (Expression elem: args.getElements0()) {
seps = seps.append(elem.accept(this));
}
return vf.constructor(RascalValueFactory.Symbol_IterSeps, arg, seps);
Expand All @@ -192,7 +192,7 @@ public IConstructor visitExpressionCallOrTree(CallOrTree x) {
StringConstant.Lexical sort = (org.rascalmpl.ast.StringConstant.Lexical)
x.getArguments().get(0).getLiteral().getStringLiteral().getConstant();
IList rest = vf.list();
for (Expression arg: ((Expression.List)args.get(1)).getElements()) {
for (Expression arg: ((Expression.List)args.get(1)).getElements0()) {
rest = rest.append(arg.accept(this));
}
return vf.constructor(RascalValueFactory.Symbol_ParameterizedSort, vf.string(sort.getString()), rest);
Expand All @@ -209,7 +209,7 @@ public IConstructor visitExpressionCallOrTree(CallOrTree x) {
if (name.equals("char-class")) {
java.util.List<Expression> args = x.getArguments();
IList ranges = vf.list();
for (Expression arg: ((Expression.List)args.get(0)).getElements()) {
for (Expression arg: ((Expression.List)args.get(0)).getElements0()) {
ranges = ranges.append(arg.accept(this));
}
return vf.constructor(RascalValueFactory.Symbol_CharClass, ranges);
Expand Down
43 changes: 31 additions & 12 deletions src/org/rascalmpl/library/List.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,17 @@ dup([3, 1, 5, 3, 1, 7, 1, 2]);
list[&T] dup(list[&T] lst)
= ([] | (ix in it) ? it : it + [ix] | &T ix <- lst);

@deprecated{Use a list index instead}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it undeprecated? don't we want to reduce the API size?

Copy link
Member Author

@jurgenvinju jurgenvinju Dec 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I received feedback that people want to keep this function. I was trying to make this PR have fewer warnings in the stdlib so that's why this snuck in there.

It's still strange to me that the declaration of a deprecated function leads to a warning on the declaration side btw. There is nothing wrong with a deprecated declaration. It's the uses which need a warning.

@javaClass{org.rascalmpl.library.Prelude}
@synopsis{A function that implements `lst[index]`}
@description{
The expression `lst[index]` has the same semantics as calling `elementAt(index)`.
}
@benefits{
* ((elementAt)) can be passed a function argument.
}
@pitfalls{
* `lst[index]` is significantly faster than `elementAt(index)`
}
java &T elementAt(list[&T] lst, int index);


Expand Down Expand Up @@ -431,9 +440,10 @@ permutations([1,2,3]);
set[list[&T]] permutations(list[&T] lst) =
permutationsBag(distribution(lst));

private set[list[&T]] permutationsBag(map[&T element, int occurs] b) =
isEmpty(b) ? {[]} :
{ [e] + rest | e <- b, rest <- permutationsBag(removeFromBag(b,e))};
private set[list[&T]] permutationsBag(map[&T element, int occurs] b)
= isEmpty(b)
? {[]}
: { [e] + rest | e <- b, rest <- permutationsBag(removeFromBag(b,e))};


@synopsis{Pop top element from list, return a tuple.}
Expand Down Expand Up @@ -480,9 +490,10 @@ list[&T] push(&T elem, list[&T] lst) = [elem] + lst;


@synopsis{Apply a function to successive elements of list and combine the results.}
@deprecated{This function is deprecated. Use a reducer expression instead, like `(init | f(it, e) | e <- lst)`.}
jurgenvinju marked this conversation as resolved.
Show resolved Hide resolved
@description{
Apply the function `fn` to successive elements of list `lst` starting with `unit`.
The function application `reducer(lst, add, 0)` has the same semantics
as the expression `(0 | add(it, e) | e <- lst)`.
}
@examples{
```rascal-shell
Expand All @@ -491,16 +502,24 @@ int add(int x, int y) { return x + y; }
reducer([10, 20, 30, 40], add, 0);
```
}
&T reducer(list[&T] lst, &T (&T, &T) fn, &T unit) = (unit | fn(it, elm) | elm <- lst);
@benefits{
* reducer can be passed as a function argument
}
@pitfalls{
* a reducer expression can be a lot faster
* reducer expressions are more versatile (allowing multiple generators and filters)
}
&T reducer(list[&T] lst, &T (&T, &T) fn, &T unit)
= (unit | fn(it, elm) | elm <- lst);

list[&T] remove(list[&T] lst, int indexToDelete) =
[ lst[i] | i <- index(lst), i != indexToDelete ];
list[&T] remove(list[&T] lst, int indexToDelete)
= [ lst[i] | i <- index(lst), i != indexToDelete ];

private map[&T element, int occurs] removeFromBag(map[&T element, int occurs] b, &T el) =
removeFromBag(b,el,1);
private map[&T element, int occurs] removeFromBag(map[&T element, int occurs] b, &T el)
= removeFromBag(b,el,1);

private map[&T element, int occurs] removeFromBag(map[&T element, int occurs] b, &T el, int nr) =
!(b[el] ?) ? b : (b[el] <= nr ? b - (el : b[el]) : b + (el : b[el] - nr));
private map[&T element, int occurs] removeFromBag(map[&T element, int occurs] b, &T el, int nr)
= !(b[el] ?) ? b : (b[el] <= nr ? b - (el : b[el]) : b + (el : b[el] - nr));


@synopsis{Reverse a list.}
Expand Down
4 changes: 2 additions & 2 deletions src/org/rascalmpl/library/Prelude.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public IValue incrementDays(IDateTime dt, IInteger n)
return incrementDate(dt, Calendar.DAY_OF_MONTH, "days", n);
}

private String getTZString(int hourOffset, int minuteOffset) {
public static String getTZString(int hourOffset, int minuteOffset) {
String tzString = "GMT" +
((hourOffset < 0 || (0 == hourOffset && minuteOffset < 0)) ? "-" : "+") +
String.format("%02d",hourOffset >= 0 ? hourOffset : hourOffset * -1) +
Expand Down Expand Up @@ -629,7 +629,7 @@ private Calendar getCalendarForTime(IDateTime inputTime) {
}
}

private Calendar getCalendarForDateTime(IDateTime inputDateTime) {
public static Calendar getCalendarForDateTime(IDateTime inputDateTime) {
if (inputDateTime.isDateTime()) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(getTZString(inputDateTime.getTimezoneOffsetHours(),inputDateTime.getTimezoneOffsetMinutes())),Locale.getDefault());
cal.setLenient(false);
Expand Down
29 changes: 0 additions & 29 deletions src/org/rascalmpl/library/lang/json/Factory.java

This file was deleted.

48 changes: 0 additions & 48 deletions src/org/rascalmpl/library/lang/json/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.util.stream.Collectors;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.exceptions.RuntimeExceptionFactory;
import org.rascalmpl.library.lang.json.internal.IValueAdapter;
import org.rascalmpl.library.lang.json.internal.JSONReadingTypeVisitor;
import org.rascalmpl.library.lang.json.internal.JsonValueReader;
import org.rascalmpl.library.lang.json.internal.JsonValueWriter;
import org.rascalmpl.types.ReifiedType;
Expand All @@ -45,13 +43,8 @@
import io.usethesource.vallang.type.Type;
import io.usethesource.vallang.type.TypeStore;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.ibm.icu.text.DateFormat;

public class IO {
private final IRascalValueFactory values;
Expand All @@ -62,48 +55,7 @@ public IO(IRascalValueFactory values, IRascalMonitor monitor) {
this.values = values;
this.monitor = monitor;
}

public IString toJSON(IValue value) {
return toJSON(value, this.values.bool(false));
}


public IString toJSON(IValue value, IBool compact) {
IValueAdapter adap = new IValueAdapter(compact.getValue());
Gson gson = new GsonBuilder()
.registerTypeAdapter(IValue.class, adap)
.enableComplexMapKeySerialization()
.setDateFormat(DateFormat.LONG)
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setVersion(1.0)
.disableHtmlEscaping()
.create();
try {
String json = gson.toJson(value, new TypeToken<IValue>() {}.getType());
return values.string(json);
} catch (Exception e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
}
}

public IValue fromJSON(IValue type, IString src) {
TypeStore store = new TypeStore();
Type start = new TypeReifier(values).valueToType((IConstructor) type, store);
Gson gson = new GsonBuilder()
.enableComplexMapKeySerialization()
.setDateFormat(DateFormat.LONG)
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setVersion(1.0)
.create();
Object obj = gson.fromJson(src.getValue(), Object.class);
try {
return JSONReadingTypeVisitor.read(obj, values, store, start);
}
catch (IOException e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null);
}
}

public IValue readJSON(IValue type, ISourceLocation loc, IString dateTimeFormat, IBool lenient, IBool trackOrigins, IFunction parsers, IMap nulls, IBool explicitConstructorNames, IBool explicitDataTypes) {
TypeStore store = new TypeStore();
Type start = new TypeReifier(values).valueToType((IConstructor) type, store);
Expand Down
Loading
Loading