Skip to content

Commit

Permalink
format code in :parser-core
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Sep 14, 2020
1 parent 63d9abd commit f0fead7
Show file tree
Hide file tree
Showing 21 changed files with 280 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class InvalidTermTypeException(
offendingSymbol,
line,
column,
"Expecting ${expected.simpleName}, got `$offendingSymbol` instead" ,
"Expecting ${expected.simpleName}, got `$offendingSymbol` instead",
cause
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ open class ParseException(

override fun toString(): String {
return "ParseException{" +
"message='" + message!!.replace("\\n", "\\\\n") + '\'' +
", line=" + line +
", column=" + column +
", offendingSymbol='" + offendingSymbol + '\'' +
'}'
"message='" + message!!.replace("\\n", "\\\\n") + '\'' +
", line=" + line +
", column=" + column +
", offendingSymbol='" + offendingSymbol + '\'' +
'}'
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package it.unibo.tuprolog.core.parsing

import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.core.Atom
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.core.Constant
import it.unibo.tuprolog.core.Integer
import it.unibo.tuprolog.core.Numeric
import it.unibo.tuprolog.core.Real
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.core.operators.Operator
import it.unibo.tuprolog.core.operators.OperatorSet
import kotlin.js.JsName
Expand All @@ -18,7 +26,6 @@ interface TermParser {
fun parseTerm(input: String): Term =
parseTerm(input, defaultOperatorSet)


@JsName("parseStructWithOperators")
fun parseStruct(input: String, operators: OperatorSet): Struct =
parseTerm(input, operators) as Struct
Expand All @@ -27,7 +34,6 @@ interface TermParser {
fun parseStruct(input: String): Struct =
parseStruct(input, defaultOperatorSet)


@JsName("parseConstantWithOperators")
fun parseConstant(input: String, operators: OperatorSet): Constant =
parseTerm(input, operators) as Constant
Expand All @@ -36,7 +42,6 @@ interface TermParser {
fun parseConstant(input: String): Constant =
parseConstant(input, defaultOperatorSet)


@JsName("parseVarWithOperators")
fun parseVar(input: String, operators: OperatorSet): Var =
parseTerm(input, operators) as Var
Expand All @@ -45,7 +50,6 @@ interface TermParser {
fun parseVar(input: String): Var =
parseVar(input, defaultOperatorSet)


@JsName("parseAtomWithOperators")
fun parseAtom(input: String, operators: OperatorSet): Atom =
parseTerm(input, operators) as Atom
Expand All @@ -54,7 +58,6 @@ interface TermParser {
fun parseAtom(input: String): Atom =
parseAtom(input, defaultOperatorSet)


@JsName("parseNumericWithOperators")
fun parseNumeric(input: String, operators: OperatorSet): Numeric =
parseTerm(input, operators) as Numeric
Expand All @@ -63,7 +66,6 @@ interface TermParser {
fun parseNumeric(input: String): Numeric =
parseNumeric(input, defaultOperatorSet)


@JsName("parseIntegerWithOperators")
fun parseInteger(input: String, operators: OperatorSet): Integer =
parseTerm(input, operators) as Integer
Expand All @@ -72,7 +74,6 @@ interface TermParser {
fun parseInteger(input: String): Integer =
parseInteger(input, defaultOperatorSet)


@JsName("parseRealWithOperators")
fun parseReal(input: String, operators: OperatorSet): Real =
parseTerm(input, operators) as Real
Expand All @@ -81,12 +82,11 @@ interface TermParser {
fun parseReal(input: String): Real =
parseReal(input, defaultOperatorSet)


@JsName("parseClauseWithOperators")
fun parseClause(input: String, operators: OperatorSet): Clause {
require(operators.any { it.functor == Clause.FUNCTOR }) {
"Error while parsing string as Clause: the provided operator set has no " +
"operator for '${Clause.FUNCTOR}'/1 or '${Clause.FUNCTOR}'/1"
"operator for '${Clause.FUNCTOR}'/1 or '${Clause.FUNCTOR}'/1"
}
return parseTerm(input, operators).toClause()
}
Expand Down Expand Up @@ -116,5 +116,4 @@ interface TermParser {
@JsName("withOperators")
fun withOperators(vararg operators: Operator) = withOperators(OperatorSet(*operators))
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

package it.unibo.tuprolog.core.parsing

import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.core.Atom
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.core.Constant
import it.unibo.tuprolog.core.Fact
import it.unibo.tuprolog.core.Integer
import it.unibo.tuprolog.core.Numeric
import it.unibo.tuprolog.core.Real
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.core.operators.OperatorSet
import kotlin.js.JsName
import kotlin.jvm.JvmName
Expand All @@ -13,7 +22,7 @@ private val defaultParser = TermParser.withDefaultOperators

@JsName("termToClause")
fun Term.toClause(source: Any? = null, line: Int = 0, column: Int = 0): Clause =
when(this) {
when (this) {
is Clause -> this
is Struct -> Fact.of(this)
else -> throw InvalidTermTypeException(source, toString(), Clause::class, line, column)
Expand Down Expand Up @@ -99,7 +108,6 @@ fun String.parseAsAtom(operators: OperatorSet): Atom =
fun String.parseAsAtom(): Atom =
defaultParser.parseAtom(this)


@JsName("parseNumericWithOperators")
fun Numeric.Companion.parse(input: String, operators: OperatorSet): Numeric =
defaultParser.parseNumeric(input, operators)
Expand All @@ -116,7 +124,6 @@ fun String.parseAsNumeric(operators: OperatorSet): Numeric =
fun String.parseAsNumeric(): Numeric =
defaultParser.parseNumeric(this)


@JsName("parseIntegerWithOperators")
fun Integer.Companion.parse(input: String, operators: OperatorSet): Integer =
defaultParser.parseInteger(input, operators)
Expand All @@ -133,7 +140,6 @@ fun String.parseAsInteger(operators: OperatorSet): Integer =
fun String.parseAsInteger(): Integer =
defaultParser.parseInteger(this)


@JsName("parseRealWithOperators")
fun Real.Companion.parse(input: String, operators: OperatorSet): Real =
defaultParser.parseReal(input, operators)
Expand Down Expand Up @@ -164,4 +170,4 @@ fun String.parseAsClause(operators: OperatorSet): Clause =

@JsName("parseStringAsClause")
fun String.parseAsClause(): Clause =
defaultParser.parseClause(this)
defaultParser.parseClause(this)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ fun assertTermsAreEqual(expected: Term, actual: Term) {
assertEquals(expected.isGround, actual.isGround)
if (expected.isGround) {
assertEquals(
expected, actual, message = """Comparing:
expected,
actual,
message =
"""Comparing:
| actual: $actual
| type: ${actual::class}
| expected: $expected
Expand Down Expand Up @@ -61,4 +64,4 @@ fun TermParser.assertTermIsCorrectlyParsed(stringToBeParsed: String, expected: T
assertTermsAreEqual(actual, expected)

if (loggingOn) println("".padEnd(80, '-'))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,83 +13,119 @@ class DoubleChecksOnEqualities {

@Test
fun testNumsAreEquals1() {
assertEquals(Integer.of(1), prolog {
1.toTerm()
})
assertEquals(
Integer.of(1),
prolog {
1.toTerm()
}
)
}

@Test
fun testRealsInitialization() {
assertEquals(Real.of(3.1), Real.of("3.1"))
assertEquals(
Real.of(3.1),
Real.of("3" + ".1")
)
}

@Test
fun testNumsAreEquals2() {
assertEquals(Integer.of(1), prolog {
numOf(1)
})
assertEquals(
Integer.of(1),
prolog {
numOf(1)
}
)
}

@Test
fun testNumsAreEquals3() {
assertEquals(Integer.of(1), prolog {
numOf(1L)
})
assertEquals(
Integer.of(1),
prolog {
numOf(1L)
}
)
}

@Test
fun testNumsAreEquals4() {
assertEquals(Integer.of(1), prolog {
numOf(BigInteger.ONE)
})
assertEquals(
Integer.of(1),
prolog {
numOf(BigInteger.ONE)
}
)
}

@Test
fun testNumsAreEquals5() {
assertEquals(Integer.of(1), prolog {
numOf(BigInteger.TEN / BigInteger.TEN)
})
assertEquals(
Integer.of(1),
prolog {
numOf(BigInteger.TEN / BigInteger.TEN)
}
)
}

@Test
fun testListsAreEquals1() {
assertEquals(Cons.singleton(Integer.of(1)), prolog {
listOf(1)
})
assertEquals(
Cons.singleton(Integer.of(1)),
prolog {
listOf(1)
}
)
}

@Test
fun testListsAreEquals2() {
assertEquals(Cons.singleton(Integer.of(1)), prolog {
listOf(numOf(1))
})
assertEquals(
Cons.singleton(Integer.of(1)),
prolog {
listOf(numOf(1))
}
)
}

@Test
fun testListsAreEquals3() {
assertEquals(Cons.singleton(Integer.of(1)), prolog {
consOf(1, emptyList)
})
assertEquals(
Cons.singleton(Integer.of(1)),
prolog {
consOf(1, emptyList)
}
)
}

@Test
fun testListsAreEquals4() {
assertEquals(Cons.singleton(Integer.of(1)), prolog {
consOf(numOf(1), emptyList)
})
assertEquals(
Cons.singleton(Integer.of(1)),
prolog {
consOf(numOf(1), emptyList)
}
)
}

@Test
fun testListsAreEquals5() {
assertEquals(Cons.singleton(Integer.of(1)), prolog {
consOf(Integer.of(1), emptyList)
})
assertEquals(
Cons.singleton(Integer.of(1)),
prolog {
consOf(Integer.of(1), emptyList)
}
)
}

@Test
fun testStructsAreEquals() {
assertEquals(Struct.of("+", Integer.of(1), Integer.of(2)), prolog {
1.toTerm() + 2
})
assertEquals(
Struct.of("+", Integer.of(1), Integer.of(2)),
prolog {
1.toTerm() + 2
}
)
}
}
}
Loading

0 comments on commit f0fead7

Please sign in to comment.