Skip to content

Commit

Permalink
bracket -> parenthesis, brackets -> parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-milella committed Jul 5, 2023
1 parent 0343aae commit 70ed0fc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 60 deletions.
4 changes: 2 additions & 2 deletions NOTE
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ to be

syntaxstring:

0) clonare e istanziare in dev-v0.9.1 (e tenerlo checkouttato lì) SoleBase e SoleData.
DONE: 0) clonare e istanziare in dev-v0.9.1 (e tenerlo checkouttato lì) SoleBase e SoleData.

1) rinominare brackets in parentheses, e bracket in paranthesis.
1) rinominare parentheses in parentheses, e parenthesis in paranthesis.
Questo va fatto in SoleLogics (dev-v0.9.1), e SoleModels (dev-v0.9.1)

2) rendere più leggibile syntaxstring (di default, NON è allparentheses.)
Expand Down
4 changes: 2 additions & 2 deletions src/general.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ isbinary(a) = arity(a) == 2
Produces the string representation of a formula or syntax token by performing
a tree traversal of the syntax tree representation of the formula.
Note that this representation may introduce redundant brackets.
Note that this representation may introduce redundant parentheses.
`kwargs` can be used to specify how to display syntax tokens/trees under
some specific conditions.
Expand Down Expand Up @@ -84,7 +84,7 @@ the function can simply be:
!!! warning
The `syntaxstring` for syntax tokens (e.g., propositions, operators) should not be
prefixed/suffixed by whitespaces, as this may cause ambiguities upon *parsing*.
For similar reasons, `syntaxstring`s should not contain brackets (`'('`, `')'`),
For similar reasons, `syntaxstring`s should not contain parentheses (`'('`, `')'`),
and, when parsing in function notation, commas (`','`).
See also [`parsebaseformula`](@ref).
Expand Down
86 changes: 43 additions & 43 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ Base.operator_precedence(::typeof(IMPLICATION)) = LOW_PRECEDENCE
const STACK_TOKEN_TYPE = Union{<:AbstractSyntaxToken,Symbol}

# Special symbols: syntax tokens cannot contain these:
const DEFAULT_OPENING_BRACKET = "("
const DEFAULT_CLOSING_BRACKET = ")"
const DEFAULT_OPENING_PARENTHESIS = "("
const DEFAULT_CLOSING_PARENTHESIS = ")"
const DEFAULT_ARG_DELIM = ","


Expand All @@ -127,12 +127,12 @@ const BASE_PARSABLE_OPERATORS = [
function _check_unary_validity(
tokens::Vector{STACK_TOKEN_TYPE},
op::AbstractOperator,
opening_bracket::Symbol,
opening_parenthesis::Symbol,
arg_delim::Symbol
)
# A unary operator is always preceeded by some other operator or a opening_bracket
# A unary operator is always preceeded by some other operator or a opening_parenthesis
if (arity(op) == 1 && !isempty(tokens) &&
(tokens[end] !== opening_bracket && tokens[end] !== arg_delim &&
(tokens[end] !== opening_parenthesis && tokens[end] !== arg_delim &&
!(tokens[end] isa AbstractOperator))
)
error("Malformed input: operator `" * syntaxstring(op) *
Expand Down Expand Up @@ -207,24 +207,24 @@ function _interpret_tokens(
raw_tokens::Vector{String},
string_to_op::Dict{String,AbstractOperator},
proposition_parser::Base.Callable;
opening_bracket::Symbol,
closing_bracket::Symbol,
opening_parenthesis::Symbol,
closing_parenthesis::Symbol,
arg_delim::Symbol
)
tokens = STACK_TOKEN_TYPE[]

i = 1
while i <= length(raw_tokens)
tok = begin
if (Symbol(raw_tokens[i]) in [opening_bracket, closing_bracket, arg_delim])
if (Symbol(raw_tokens[i]) in [opening_parenthesis, closing_parenthesis, arg_delim])
# If the token is a special symbol -> push it as is
Symbol(raw_tokens[i])
else
st = syntaxstring(raw_tokens[i])
if (st in keys(string_to_op))
# If the token is an operator -> perform check and push it as is
op = string_to_op[st]
_check_unary_validity(tokens, op, opening_bracket, arg_delim)
_check_unary_validity(tokens, op, opening_parenthesis, arg_delim)
op
else
# If the token is something else -> parse as Proposition and push it
Expand All @@ -249,8 +249,8 @@ function tokenizer(
operators::Vector{<:AbstractOperator},
proposition_parser::Base.Callable,
additional_whitespaces::Vector{Char},
opening_bracket::Symbol = Symbol(DEFAULT_OPENING_BRACKET),
closing_bracket::Symbol = Symbol(DEFAULT_CLOSING_BRACKET),
opening_parenthesis::Symbol = Symbol(DEFAULT_OPENING_PARENTHESIS),
closing_parenthesis::Symbol = Symbol(DEFAULT_CLOSING_PARENTHESIS),
arg_delim::Symbol = Symbol(DEFAULT_ARG_DELIM),
)
# Strip input's whitespaces
Expand All @@ -269,7 +269,7 @@ function tokenizer(

# Each parsing method has to know which symbols represent opening/closing a context;
# additionaly, parsing in function notation needs to know how arguments are separated.
special_delimiters = vcat(opening_bracket, closing_bracket)
special_delimiters = vcat(opening_parenthesis, closing_parenthesis)
if !(isnothing(arg_delim))
push!(special_delimiters, arg_delim)
end
Expand All @@ -282,29 +282,29 @@ function tokenizer(

# Interpret each raw token
return _interpret_tokens(raw_tokens, string_to_op, proposition_parser;
opening_bracket = opening_bracket, closing_bracket = closing_bracket,
opening_parenthesis = opening_parenthesis, closing_parenthesis = closing_parenthesis,
arg_delim = arg_delim)
end

# Rearrange a serie of token, from infix to postfix notation
function shunting_yard!(
tokens::Vector{STACK_TOKEN_TYPE};
opening_bracket::Symbol = Symbol(DEFAULT_OPENING_BRACKET),
closing_bracket::Symbol = Symbol(DEFAULT_CLOSING_BRACKET))
opening_parenthesis::Symbol = Symbol(DEFAULT_OPENING_PARENTHESIS),
closing_parenthesis::Symbol = Symbol(DEFAULT_CLOSING_PARENTHESIS))
tokstack = STACK_TOKEN_TYPE[] # support structure
postfix = AbstractSyntaxToken[] # returned structure: tokens rearranged in postfix

for tok in tokens
if tok isa Symbol
# If tok is a Symbol, then it might be a special parsing symbol
if tok === opening_bracket
if tok === opening_parenthesis
# Start a new "context" in the expression
push!(tokstack, tok)
elseif tok === closing_bracket
elseif tok === closing_parenthesis
# `tokstack` shrinks and postfix vector is filled
while !isempty(tokstack)
popped = pop!(tokstack)
if popped !== opening_bracket
if popped !== opening_parenthesis
push!(postfix, popped)
else
break
Expand Down Expand Up @@ -336,9 +336,9 @@ function shunting_yard!(
while !isempty(tokstack)
popped = pop!(tokstack)

# Starting expression is not well formatted, or a opening_bracket is found
# Starting expression is not well formatted, or a opening_parenthesis is found
if !(popped isa AbstractOperator)
error("Parsing error! Mismatching brackets detected.")
error("Parsing error! Mismatching parentheses detected.")
end
push!(postfix, popped)
end
Expand All @@ -353,8 +353,8 @@ end
function_notation::Bool = false,
proposition_parser::Base.Callable = Proposition{String},
additional_whitespaces::Vector{Char} = Char[],
opening_bracket::String = $(repr(DEFAULT_OPENING_BRACKET)),
closing_bracket::String = $(repr(DEFAULT_CLOSING_BRACKET)),
opening_parenthesis::String = $(repr(DEFAULT_OPENING_PARENTHESIS)),
closing_parenthesis::String = $(repr(DEFAULT_CLOSING_PARENTHESIS)),
arg_delim::String = $(repr(DEFAULT_ARG_DELIM))
)
Expand Down Expand Up @@ -384,9 +384,9 @@ a second argument.
- `additional_whitespaces`::Vector{Char} = Char[]: characters to be stripped out from each
syntax token.
For example, if `'@' in additional_whitespaces`, "¬@p@" is parsed just as "¬p".
- `opening_bracket`::String = $(repr(DEFAULT_OPENING_BRACKET)):
- `opening_parenthesis`::String = $(repr(DEFAULT_OPENING_PARENTHESIS)):
the string signaling the opening of an expression block;
- `closing_bracket`::String = $(repr(DEFAULT_CLOSING_BRACKET)):
- `closing_parenthesis`::String = $(repr(DEFAULT_CLOSING_PARENTHESIS)):
the string signaling the closing of an expression block;
- `arg_delim`::String = $(repr(DEFAULT_ARG_DELIM)):
when `function_notation = true`,
Expand All @@ -398,7 +398,7 @@ a second argument.
For example, for any operator `⨁`,
it should hold that `syntaxstring(⨁) == strip(syntaxstring(⨁))`.
Also, `syntaxstring`s cannot contain special symbols
(`opening_bracket`, `closing_bracket`, and `arg_delim`) as
(`opening_parenthesis`, `closing_parenthesis`, and `arg_delim`) as
substrings.
# Examples
Expand All @@ -425,8 +425,8 @@ function parseformula(
function_notation::Bool = false,
proposition_parser::Base.Callable = Proposition{String},
additional_whitespaces::Vector{Char} = Char[],
opening_bracket::String = DEFAULT_OPENING_BRACKET,
closing_bracket::String = DEFAULT_CLOSING_BRACKET,
opening_parenthesis::String = DEFAULT_OPENING_PARENTHESIS,
closing_parenthesis::String = DEFAULT_CLOSING_PARENTHESIS,
arg_delim::String = DEFAULT_ARG_DELIM,
)
additional_operators = (
Expand All @@ -437,22 +437,22 @@ function parseformula(
# TODO: expand special sequences to special *sequences* (strings of characters)
# TODO: check that no special sequence is a substring of another one.
@assert function_notation ||
(allunique(string.([opening_bracket, arg_delim])) ||
allunique(string.([closing_bracket, arg_delim]))) "" *
(allunique(string.([opening_parenthesis, arg_delim])) ||
allunique(string.([closing_parenthesis, arg_delim]))) "" *
"Invalid " *
"special sequences provided: please, check that both the `opening_bracket` " *
"and the `closing_bracket` are not equal to the `arg_delim`."
"special sequences provided: please, check that both the `opening_parenthesis` " *
"and the `closing_parenthesis` are not equal to the `arg_delim`."

opening_bracket = Symbol(opening_bracket)
closing_bracket = Symbol(closing_bracket)
opening_parenthesis = Symbol(opening_parenthesis)
closing_parenthesis = Symbol(closing_parenthesis)
arg_delim = Symbol(arg_delim)

# parsetree workflow:
# 1) function_notation = false; _infixbuild -> _postfixbuild
# 2) function_notation = true; _fxbuild -> _prefixbuild

# Build a formula starting from its postfix notation, preprocessed with shunting yard.
# In other words, all special symbols (e.g. opening_bracket) are already filtered
# In other words, all special symbols (e.g. opening_parenthesis) are already filtered
# out and only AbstractSyntaxToken are considered.
function _postfixbuild(postfix::Vector{<:AbstractSyntaxToken})

Expand Down Expand Up @@ -483,9 +483,9 @@ function parseformula(
# actually this is a preprocessing who fallbacks into `_postfixbuild`
function _infixbuild()
tokens = tokenizer(expression, operators, proposition_parser,
additional_whitespaces, opening_bracket, closing_bracket)
additional_whitespaces, opening_parenthesis, closing_parenthesis)
return _postfixbuild(shunting_yard!(tokens,
opening_bracket = opening_bracket, closing_bracket = closing_bracket))
opening_parenthesis = opening_parenthesis, closing_parenthesis = closing_parenthesis))
end

# Build a formula starting from its function notation;
Expand All @@ -504,8 +504,8 @@ function parseformula(
push!(stack, newtok)
elseif (length(stack) >= (1 + 2*arity(tok)))
# Else, follow this general procedure;
# consider 1 opening bracket, `arity` AbstractSyntaxToken,
# `arity`-1 delims and 1 closing bracket for a total of
# consider 1 opening parenthesis, `arity` AbstractSyntaxToken,
# `arity`-1 delims and 1 closing parenthesis for a total of
# 1 + (arity) + (arity-1) + 1 = (1 + 2*arity) tokens to read.
#
# ( T , T , ... , T )
Expand All @@ -517,8 +517,8 @@ function parseformula(
stack = stack[1:(length(stack) - 1 - 2*arity(tok))]

# The following conditions must hold
# stack[1] == OPENING BRACKET
# stack[end] == CLOSING BRACKET
# stack[1] == OPENING PARENTHESIS
# stack[end] == CLOSING PARENTHESIS
# stack[even indexes] <: AbstractSyntaxTree
# stack[odd indexes after 1 and before length(stack)] == SEP
# else an error has to be thrown
Expand All @@ -529,8 +529,8 @@ function parseformula(
delims =
[s for s in 3:(length(popped)-2) if popped[s] == arg_delim]

if (popped[1] == opening_bracket &&
popped[end] == closing_bracket &&
if (popped[1] == opening_parenthesis &&
popped[end] == closing_parenthesis &&
length(children) == arity(tok) &&
length(delims) == arity(tok) - 1)
push!(stack, SyntaxTree(tok, Tuple(children)))
Expand Down Expand Up @@ -561,7 +561,7 @@ function parseformula(
# actually this is a preprocessing who fallbacks into `_prefixbuild`
function _fxbuild()
tokens = tokenizer(expression, operators, proposition_parser,
additional_whitespaces, opening_bracket, closing_bracket, arg_delim)
additional_whitespaces, opening_parenthesis, closing_parenthesis, arg_delim)
return _prefixbuild(tokens)
end

Expand Down
26 changes: 13 additions & 13 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import SoleLogics: arity
@test_nowarn parsetree("¬p∧q∧(¬s∧¬z)", [NEGATION, CONJUNCTION])
@test_nowarn parsetree("¬p∧q∧(¬s∧¬z)", [NEGATION])
@test_nowarn parsetree("¬p∧q∧{¬s∧¬z}",
opening_bracket="{", closing_bracket="}")
opening_parenthesis="{", closing_parenthesis="}")
@test_nowarn parsetree("¬p∧q∧ A ¬s∧¬z B",
opening_bracket="A", closing_bracket="B")
opening_parenthesis="A", closing_parenthesis="B")

@test operatorstype(
logic(parsebaseformula("¬p∧q∧(¬s∧¬z)", [BOX]))) <: SoleLogics.BaseModalOperators
Expand Down Expand Up @@ -54,16 +54,16 @@ import SoleLogics: arity
@test_nowarn parsetree("→(∧(¬p; q); ∧(¬s; ¬z))",
function_notation=true, arg_delim = ";")
@test_nowarn parsetree("→{∧{¬p; q}; ∧{¬s; ¬z}}", function_notation=true,
opening_bracket = "{", closing_bracket = "}",
opening_parenthesis = "{", closing_parenthesis = "}",
arg_delim = ";")


@test filter(!isspace, syntaxstring(parsetree("¬p∧q→(¬s∧¬z)");
function_notation = true)) == "→(∧(¬(p),q),∧(¬(s),¬(z)))"
@test filter(!isspace, syntaxstring(
parsetree("¬p∧q→A¬s∧¬zB",
opening_bracket = "A",
closing_bracket = "B");
opening_parenthesis = "A",
closing_parenthesis = "B");
function_notation = true)) == "→(∧(¬(p),q),∧(¬(s),¬(z)))"
@test_nowarn parsetree("¬p∧q→ (¬s∧¬z)")
@test parsetree("□p∧ q∧(□s∧◊z)", [BOX]) == parsetree("□p∧ q∧(□s∧◊z)")
Expand Down Expand Up @@ -119,14 +119,14 @@ f = parsetree("⟨G⟩(((¬(⟨G⟩((q ∧ p) → (¬(q))))) ∧ (((¬(q → q))
function_notation = true)
@test_throws ErrorException parsetree("¬[[G]]p"; function_notation = true)

@test_throws ErrorException parsetree("¬p∧q∧(¬s∧¬z)", opening_bracket="{")
@test_throws ErrorException parsetree("¬p∧q∧(¬s∧¬z)", opening_parenthesis="{")
@test_throws ErrorException parsetree("¬p∧q∧{¬s∧¬z)",
opening_bracket="{", closing_bracket="}")
opening_parenthesis="{", closing_parenthesis="}")
@test_throws ErrorException parsetree("¬p∧q∧ C ¬s∧¬z B",
opening_bracket="A", closing_bracket="B")
opening_parenthesis="A", closing_parenthesis="B")

@test_throws ErrorException parsetree("¬p∧q→ |¬s∧¬z|",
opening_bracket = "|", closing_bracket = "|")
opening_parenthesis = "|", closing_parenthesis = "|")

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ parsing propositions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -202,15 +202,15 @@ SoleLogics.syntaxstring(op::CurlyRelationalOperator; kwargs...) =
struct MyCustomRelationalOperator{R<:AbstractRelation} <: AbstractRelationalOperator{R} end
(MyCustomRelationalOperator)(r::AbstractRelation) = MyCustomRelationalOperator{typeof(r)}()
SoleLogics.syntaxstring(op::MyCustomRelationalOperator; kwargs...) =
"LEFT CUSTOM BRACKET $(syntaxstring(relationtype(op); kwargs...)) RIGHT CUSTOM BRACKET"
f = parsetree("LEFT CUSTOM BRACKET G RIGHT CUSTOM BRACKET p ∧ ¬" *
"LEFT CUSTOM BRACKET G RIGHT CUSTOM BRACKET q", [MyCustomRelationalOperator(globalrel)])
"LEFT CUSTOM PARENTHESIS $(syntaxstring(relationtype(op); kwargs...)) RIGHT CUSTOM PARENTHESIS"
f = parsetree("LEFT CUSTOM PARENTHESIS G RIGHT CUSTOM PARENTHESIS p ∧ ¬" *
"LEFT CUSTOM PARENTHESIS G RIGHT CUSTOM PARENTHESIS q", [MyCustomRelationalOperator(globalrel)])

@test_nowarn parsetree("🌅G🌄p ∧ ¬🌅G🌄q", [SoleRelationalOperator(globalrel)])
@test_nowarn parsetree("∧(🌅G🌄p,¬🌅G🌄q)", [SoleRelationalOperator(globalrel)];
function_notation = true)
@test_nowarn parsetree("∧[🌅G🌄p DELIM ¬🌅G🌄q)", [SoleRelationalOperator(globalrel)];
function_notation = true, opening_bracket = "[", arg_delim = "DELIM")
function_notation = true, opening_parenthesis = "[", arg_delim = "DELIM")

@test_nowarn parsetree("|G|p ∧ ¬|G|q", [PipeRelationalOperator(globalrel)])
@test_nowarn parsetree("∧(|G|p, ¬|G|q)", [PipeRelationalOperator(globalrel)];
Expand Down

0 comments on commit 70ed0fc

Please sign in to comment.