Navigation Menu

Skip to content

Commit

Permalink
Terminate SMARTS parsing on whitespace or EOF.
Browse files Browse the repository at this point in the history
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Dec 14, 2014
1 parent d595bea commit 84c89ca
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 60 deletions.
Expand Up @@ -11,7 +11,7 @@ options {

PARSER_BEGIN(SMARTSParser)

/* Copyright (C) 2004-2007 The Chemistry Development Kit (CDK) project
/* Copyright (C) 2004-2007 The Chemistry Development Kit (CDK) project
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand All @@ -30,7 +30,7 @@ PARSER_BEGIN(SMARTSParser)
*/
package org.openscience.cdk.smiles.smarts.parser;

import java.io.StringReader;
import java.io.StringReader;
import org.openscience.cdk.isomorphism.matchers.QueryAtomContainer;
import org.openscience.cdk.interfaces.IChemObjectBuilder;

Expand Down Expand Up @@ -70,9 +70,9 @@ import org.openscience.cdk.interfaces.IChemObjectBuilder;
* @cdk.keyword substructure search
*/
public class SMARTSParser {

private int componentId = 0;

private int componentId = 0;
/**
* This method parses a Smarts String and returns an instance of
* <code>QueryAtomContainer</code>
Expand Down Expand Up @@ -102,11 +102,11 @@ TOKEN_MGR_DECLS : {
// Required by SetState
void backup(int n) { input_stream.backup(n); }

}
}

<*> TOKEN:
{
<#_WS: ( " " | "\t" | "\n" | "\r" ) >
<WS: ( " " | "\t" | "\n" | "\r" ) >
|
// Logical Operators
<L_AND : ";" >
Expand Down Expand Up @@ -294,7 +294,7 @@ TOKEN_MGR_DECLS : {
}

/**
* Start ::= <ReactionExpression>
* Start ::= <ReactionExpression> <#_WS>
* ReactionExpression ::= <GroupExpression> (">>" <GroupExpression>)? |
* ">" <GroupExpression> ">" | ">>" <GroupExpression>
* GroupExpression ::= ["("] <SmartsExpresion> [")"] ( "." ["("] <SmartsExpression> [")"] )*
Expand Down Expand Up @@ -333,7 +333,7 @@ TOKEN_MGR_DECLS : {

ASTStart Start() #Start : {}
{
ReactionExpression() <EOF>
ReactionExpression() (<WS> | <EOF>)
{
return jjtThis;
}
Expand All @@ -348,74 +348,74 @@ void ReactionExpression() #Reaction : {}
">>" GroupExpression()
}

void GroupExpression() #Group : {}
{
(
<L_PAREN>
SmartsExpression()
{
((ASTSmarts) jjtree.peekNode()).setComponentId(++componentId);
}
( "."
SmartsExpression()
{
// same component grouping
((ASTSmarts) jjtree.peekNode()).setComponentId(componentId);
}
)*
<R_PAREN>
|
SmartsExpression()
)
void GroupExpression() #Group : {}
{
(
<L_PAREN>
SmartsExpression()
{
((ASTSmarts) jjtree.peekNode()).setComponentId(++componentId);
}
( "."
SmartsExpression()
{
// same component grouping
((ASTSmarts) jjtree.peekNode()).setComponentId(componentId);
}
)*
<R_PAREN>
|
SmartsExpression()
)
( "."
(
<L_PAREN>
SmartsExpression()
{
((ASTSmarts) jjtree.peekNode()).setComponentId(++componentId);
}
( "."
SmartsExpression()
{
// same component grouping
((ASTSmarts) jjtree.peekNode()).setComponentId(componentId);
}
)*
<R_PAREN>
|
SmartsExpression()
)
(
<L_PAREN>
SmartsExpression()
{
((ASTSmarts) jjtree.peekNode()).setComponentId(++componentId);
}
( "."
SmartsExpression()
{
// same component grouping
((ASTSmarts) jjtree.peekNode()).setComponentId(componentId);
}
)*
<R_PAREN>
|
SmartsExpression()
)
)*
}

void SmartsExpression() #Smarts :
{
ASTAtom atom;
int ringIdToken;
ASTAtom atom;
int ringIdToken;
}
{
atom = AtomExpression()
(
(
( LowAndBond() )?
(
(<DIGIT>
{ ringIdToken = token.image.charAt(0) - '0'; }
| ("%" <DIGIT>
{ ringIdToken = 10 * (token.image.charAt(0) - '0'); }
<DIGIT>
{ ringIdToken += token.image.charAt(0) - '0'; }))
(<DIGIT>
{ ringIdToken = token.image.charAt(0) - '0'; }
| ("%" <DIGIT>
{ ringIdToken = 10 * (token.image.charAt(0) - '0'); }
<DIGIT>
{ ringIdToken += token.image.charAt(0) - '0'; }))
{
ASTLowAndBond bond = null;
ASTRingIdentifier ringId = new ASTRingIdentifier(JJTRINGIDENTIFIER);
if (jjtree.peekNode() instanceof ASTLowAndBond) {
bond = (ASTLowAndBond)jjtree.popNode(); // pop the bond
ringId.jjtAddChild(bond, 0);
}
ringId.setRingId(ringIdToken);
ringId.setRingId(ringIdToken);
atom.jjtAddChild(ringId, atom.jjtGetNumChildren());
}
|
}
|
atom = AtomExpression()
)
)
Expand Down Expand Up @@ -573,7 +573,7 @@ void NotExpression() #NotExpression :
void RecursiveSmartsExpression() #RecursiveSmartsExpression : {}
{
<DOLLAR> <L_PAREN> {token_source.SwitchTo(SMARTSParserConstants.DEFAULT); }
GroupExpression() <R_PAREN> {token_source.SwitchTo(SMARTSParserConstants.ATOM_EXPRESSION); }
GroupExpression() <R_PAREN> {token_source.SwitchTo(SMARTSParserConstants.ATOM_EXPRESSION); }
}

void PrimitiveAtomExpression() : {}
Expand Down Expand Up @@ -770,9 +770,9 @@ void RingIdentifier() #RingIdentifier: {}
void Chirality() #Chirality :
{ StringBuffer digits = new StringBuffer(); }
{
"@" { jjtThis.setClockwise(false); }
(
"@" { jjtThis.setClockwise(true); }
"@" { jjtThis.setClockwise(false); }
(
"@" { jjtThis.setClockwise(true); }
)?
(
<Q_MARK> { jjtThis.setUnspecified(true); }
Expand Down
Expand Up @@ -1484,5 +1484,21 @@ public void testPeriodicGroupNumber7() throws Exception {
public void testGroup5Elements() throws Exception {
parse("[V,Cr,Mn,Nb,Mo,Tc,Ta,W,Re]");
}

@Test public void endOnSpace() throws Exception {
parse("C ");
}

@Test public void endOnTab() throws Exception {
parse("C\t");
}

@Test public void endOnNewline() throws Exception {
parse("C\n");
}

@Test public void endOnCarriageReturn() throws Exception {
parse("C\r");
}

}

0 comments on commit 84c89ca

Please sign in to comment.