Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
a0bdc698
Commit
a0bdc698
authored
Feb 13, 2015
by
peastman
Browse files
Merge pull request #808 from peastman/parse
Lepton exceptions have more informative messages
parents
e9b7c563
418ceabd
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
46 deletions
+51
-46
libraries/lepton/src/ExpressionTreeNode.cpp
libraries/lepton/src/ExpressionTreeNode.cpp
+5
-5
libraries/lepton/src/Parser.cpp
libraries/lepton/src/Parser.cpp
+46
-41
No files found.
libraries/lepton/src/ExpressionTreeNode.cpp
View file @
a0bdc698
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* *
* Portions copyright (c) 2009 Stanford University and the Authors.
*
* Portions copyright (c) 2009
-2015
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Authors: Peter Eastman *
* Contributors: *
* Contributors: *
* *
* *
...
@@ -38,25 +38,25 @@ using namespace std;
...
@@ -38,25 +38,25 @@ using namespace std;
ExpressionTreeNode
::
ExpressionTreeNode
(
Operation
*
operation
,
const
vector
<
ExpressionTreeNode
>&
children
)
:
operation
(
operation
),
children
(
children
)
{
ExpressionTreeNode
::
ExpressionTreeNode
(
Operation
*
operation
,
const
vector
<
ExpressionTreeNode
>&
children
)
:
operation
(
operation
),
children
(
children
)
{
if
(
operation
->
getNumArguments
()
!=
children
.
size
())
if
(
operation
->
getNumArguments
()
!=
children
.
size
())
throw
Exception
(
"
Parse error:
wrong number of arguments to function: "
+
operation
->
getName
());
throw
Exception
(
"wrong number of arguments to function: "
+
operation
->
getName
());
}
}
ExpressionTreeNode
::
ExpressionTreeNode
(
Operation
*
operation
,
const
ExpressionTreeNode
&
child1
,
const
ExpressionTreeNode
&
child2
)
:
operation
(
operation
)
{
ExpressionTreeNode
::
ExpressionTreeNode
(
Operation
*
operation
,
const
ExpressionTreeNode
&
child1
,
const
ExpressionTreeNode
&
child2
)
:
operation
(
operation
)
{
children
.
push_back
(
child1
);
children
.
push_back
(
child1
);
children
.
push_back
(
child2
);
children
.
push_back
(
child2
);
if
(
operation
->
getNumArguments
()
!=
children
.
size
())
if
(
operation
->
getNumArguments
()
!=
children
.
size
())
throw
Exception
(
"
Parse error:
wrong number of arguments to function: "
+
operation
->
getName
());
throw
Exception
(
"wrong number of arguments to function: "
+
operation
->
getName
());
}
}
ExpressionTreeNode
::
ExpressionTreeNode
(
Operation
*
operation
,
const
ExpressionTreeNode
&
child
)
:
operation
(
operation
)
{
ExpressionTreeNode
::
ExpressionTreeNode
(
Operation
*
operation
,
const
ExpressionTreeNode
&
child
)
:
operation
(
operation
)
{
children
.
push_back
(
child
);
children
.
push_back
(
child
);
if
(
operation
->
getNumArguments
()
!=
children
.
size
())
if
(
operation
->
getNumArguments
()
!=
children
.
size
())
throw
Exception
(
"
Parse error:
wrong number of arguments to function: "
+
operation
->
getName
());
throw
Exception
(
"wrong number of arguments to function: "
+
operation
->
getName
());
}
}
ExpressionTreeNode
::
ExpressionTreeNode
(
Operation
*
operation
)
:
operation
(
operation
)
{
ExpressionTreeNode
::
ExpressionTreeNode
(
Operation
*
operation
)
:
operation
(
operation
)
{
if
(
operation
->
getNumArguments
()
!=
children
.
size
())
if
(
operation
->
getNumArguments
()
!=
children
.
size
())
throw
Exception
(
"
Parse error:
wrong number of arguments to function: "
+
operation
->
getName
());
throw
Exception
(
"wrong number of arguments to function: "
+
operation
->
getName
());
}
}
ExpressionTreeNode
::
ExpressionTreeNode
(
const
ExpressionTreeNode
&
node
)
:
operation
(
&
node
.
getOperation
()
==
NULL
?
NULL
:
node
.
getOperation
().
clone
()),
children
(
node
.
getChildren
())
{
ExpressionTreeNode
::
ExpressionTreeNode
(
const
ExpressionTreeNode
&
node
)
:
operation
(
&
node
.
getOperation
()
==
NULL
?
NULL
:
node
.
getOperation
().
clone
()),
children
(
node
.
getChildren
())
{
...
...
libraries/lepton/src/Parser.cpp
View file @
a0bdc698
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* *
* Portions copyright (c) 2009-201
3
Stanford University and the Authors. *
* Portions copyright (c) 2009-201
5
Stanford University and the Authors. *
* Authors: Peter Eastman *
* Authors: Peter Eastman *
* Contributors: *
* Contributors: *
* *
* *
...
@@ -150,6 +150,7 @@ ParsedExpression Parser::parse(const string& expression) {
...
@@ -150,6 +150,7 @@ ParsedExpression Parser::parse(const string& expression) {
}
}
ParsedExpression
Parser
::
parse
(
const
string
&
expression
,
const
map
<
string
,
CustomFunction
*>&
customFunctions
)
{
ParsedExpression
Parser
::
parse
(
const
string
&
expression
,
const
map
<
string
,
CustomFunction
*>&
customFunctions
)
{
try
{
// First split the expression into subexpressions.
// First split the expression into subexpressions.
string
primaryExpression
=
expression
;
string
primaryExpression
=
expression
;
...
@@ -170,15 +171,15 @@ ParsedExpression Parser::parse(const string& expression, const map<string, Custo
...
@@ -170,15 +171,15 @@ ParsedExpression Parser::parse(const string& expression, const map<string, Custo
for
(
int
i
=
0
;
i
<
(
int
)
subexpressions
.
size
();
i
++
)
{
for
(
int
i
=
0
;
i
<
(
int
)
subexpressions
.
size
();
i
++
)
{
string
::
size_type
equalsPos
=
subexpressions
[
i
].
find
(
'='
);
string
::
size_type
equalsPos
=
subexpressions
[
i
].
find
(
'='
);
if
(
equalsPos
==
string
::
npos
)
if
(
equalsPos
==
string
::
npos
)
throw
Exception
(
"
Parse error:
subexpression does not specify a name"
);
throw
Exception
(
"subexpression does not specify a name"
);
string
name
=
trim
(
subexpressions
[
i
].
substr
(
0
,
equalsPos
));
string
name
=
trim
(
subexpressions
[
i
].
substr
(
0
,
equalsPos
));
if
(
name
.
size
()
==
0
)
if
(
name
.
size
()
==
0
)
throw
Exception
(
"
Parse error:
subexpression does not specify a name"
);
throw
Exception
(
"subexpression does not specify a name"
);
vector
<
ParseToken
>
tokens
=
tokenize
(
subexpressions
[
i
].
substr
(
equalsPos
+
1
));
vector
<
ParseToken
>
tokens
=
tokenize
(
subexpressions
[
i
].
substr
(
equalsPos
+
1
));
int
pos
=
0
;
int
pos
=
0
;
subexpDefs
[
name
]
=
parsePrecedence
(
tokens
,
pos
,
customFunctions
,
subexpDefs
,
0
);
subexpDefs
[
name
]
=
parsePrecedence
(
tokens
,
pos
,
customFunctions
,
subexpDefs
,
0
);
if
(
pos
!=
tokens
.
size
())
if
(
pos
!=
tokens
.
size
())
throw
Exception
(
"
Parse error:
unexpected text at end of subexpression: "
+
tokens
[
pos
].
getText
());
throw
Exception
(
"unexpected text at end of subexpression: "
+
tokens
[
pos
].
getText
());
}
}
// Now parse the primary expression.
// Now parse the primary expression.
...
@@ -187,14 +188,18 @@ ParsedExpression Parser::parse(const string& expression, const map<string, Custo
...
@@ -187,14 +188,18 @@ ParsedExpression Parser::parse(const string& expression, const map<string, Custo
int
pos
=
0
;
int
pos
=
0
;
ExpressionTreeNode
result
=
parsePrecedence
(
tokens
,
pos
,
customFunctions
,
subexpDefs
,
0
);
ExpressionTreeNode
result
=
parsePrecedence
(
tokens
,
pos
,
customFunctions
,
subexpDefs
,
0
);
if
(
pos
!=
tokens
.
size
())
if
(
pos
!=
tokens
.
size
())
throw
Exception
(
"
Parse error:
unexpected text at end of expression: "
+
tokens
[
pos
].
getText
());
throw
Exception
(
"unexpected text at end of expression: "
+
tokens
[
pos
].
getText
());
return
ParsedExpression
(
result
);
return
ParsedExpression
(
result
);
}
catch
(
Exception
&
ex
)
{
throw
Exception
(
"Parse error in expression
\"
"
+
expression
+
"
\"
: "
+
ex
.
what
());
}
}
}
ExpressionTreeNode
Parser
::
parsePrecedence
(
const
vector
<
ParseToken
>&
tokens
,
int
&
pos
,
const
map
<
string
,
CustomFunction
*>&
customFunctions
,
ExpressionTreeNode
Parser
::
parsePrecedence
(
const
vector
<
ParseToken
>&
tokens
,
int
&
pos
,
const
map
<
string
,
CustomFunction
*>&
customFunctions
,
const
map
<
string
,
ExpressionTreeNode
>&
subexpressionDefs
,
int
precedence
)
{
const
map
<
string
,
ExpressionTreeNode
>&
subexpressionDefs
,
int
precedence
)
{
if
(
pos
==
tokens
.
size
())
if
(
pos
==
tokens
.
size
())
throw
Exception
(
"
Parse error:
unexpected end of expression"
);
throw
Exception
(
"unexpected end of expression"
);
// Parse the next value (number, variable, function, parenthesized expression)
// Parse the next value (number, variable, function, parenthesized expression)
...
@@ -220,7 +225,7 @@ ExpressionTreeNode Parser::parsePrecedence(const vector<ParseToken>& tokens, int
...
@@ -220,7 +225,7 @@ ExpressionTreeNode Parser::parsePrecedence(const vector<ParseToken>& tokens, int
pos
++
;
pos
++
;
result
=
parsePrecedence
(
tokens
,
pos
,
customFunctions
,
subexpressionDefs
,
0
);
result
=
parsePrecedence
(
tokens
,
pos
,
customFunctions
,
subexpressionDefs
,
0
);
if
(
pos
==
tokens
.
size
()
||
tokens
[
pos
].
getType
()
!=
ParseToken
::
RightParen
)
if
(
pos
==
tokens
.
size
()
||
tokens
[
pos
].
getType
()
!=
ParseToken
::
RightParen
)
throw
Exception
(
"
Parse error:
unbalanced parentheses"
);
throw
Exception
(
"unbalanced parentheses"
);
pos
++
;
pos
++
;
}
}
else
if
(
token
.
getType
()
==
ParseToken
::
Function
)
{
else
if
(
token
.
getType
()
==
ParseToken
::
Function
)
{
...
@@ -234,7 +239,7 @@ ExpressionTreeNode Parser::parsePrecedence(const vector<ParseToken>& tokens, int
...
@@ -234,7 +239,7 @@ ExpressionTreeNode Parser::parsePrecedence(const vector<ParseToken>& tokens, int
pos
++
;
pos
++
;
}
while
(
moreArgs
);
}
while
(
moreArgs
);
if
(
pos
==
tokens
.
size
()
||
tokens
[
pos
].
getType
()
!=
ParseToken
::
RightParen
)
if
(
pos
==
tokens
.
size
()
||
tokens
[
pos
].
getType
()
!=
ParseToken
::
RightParen
)
throw
Exception
(
"
Parse error:
unbalanced parentheses"
);
throw
Exception
(
"unbalanced parentheses"
);
pos
++
;
pos
++
;
Operation
*
op
=
getFunctionOperation
(
token
.
getText
(),
customFunctions
);
Operation
*
op
=
getFunctionOperation
(
token
.
getText
(),
customFunctions
);
try
{
try
{
...
@@ -251,7 +256,7 @@ ExpressionTreeNode Parser::parsePrecedence(const vector<ParseToken>& tokens, int
...
@@ -251,7 +256,7 @@ ExpressionTreeNode Parser::parsePrecedence(const vector<ParseToken>& tokens, int
result
=
ExpressionTreeNode
(
new
Operation
::
Negate
(),
toNegate
);
result
=
ExpressionTreeNode
(
new
Operation
::
Negate
(),
toNegate
);
}
}
else
else
throw
Exception
(
"
Parse error:
unexpected token: "
+
token
.
getText
());
throw
Exception
(
"unexpected token: "
+
token
.
getText
());
// Now deal with the next binary operator.
// Now deal with the next binary operator.
...
@@ -288,7 +293,7 @@ Operation* Parser::getOperatorOperation(const std::string& name) {
...
@@ -288,7 +293,7 @@ Operation* Parser::getOperatorOperation(const std::string& name) {
case
Operation
::
POWER
:
case
Operation
::
POWER
:
return
new
Operation
::
Power
();
return
new
Operation
::
Power
();
default:
default:
throw
Exception
(
"
Parse error:
unknown operator"
);
throw
Exception
(
"unknown operator"
);
}
}
}
}
...
@@ -334,7 +339,7 @@ Operation* Parser::getFunctionOperation(const std::string& name, const map<strin
...
@@ -334,7 +339,7 @@ Operation* Parser::getFunctionOperation(const std::string& name, const map<strin
map
<
string
,
Operation
::
Id
>::
const_iterator
iter
=
opMap
.
find
(
trimmed
);
map
<
string
,
Operation
::
Id
>::
const_iterator
iter
=
opMap
.
find
(
trimmed
);
if
(
iter
==
opMap
.
end
())
if
(
iter
==
opMap
.
end
())
throw
Exception
(
"
Parse error:
unknown function: "
+
trimmed
);
throw
Exception
(
"unknown function: "
+
trimmed
);
switch
(
iter
->
second
)
{
switch
(
iter
->
second
)
{
case
Operation
::
SQRT
:
case
Operation
::
SQRT
:
return
new
Operation
::
Sqrt
();
return
new
Operation
::
Sqrt
();
...
@@ -387,6 +392,6 @@ Operation* Parser::getFunctionOperation(const std::string& name, const map<strin
...
@@ -387,6 +392,6 @@ Operation* Parser::getFunctionOperation(const std::string& name, const map<strin
case
Operation
::
ABS
:
case
Operation
::
ABS
:
return
new
Operation
::
Abs
();
return
new
Operation
::
Abs
();
default:
default:
throw
Exception
(
"
Parse error:
unknown function"
);
throw
Exception
(
"unknown function"
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment