1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Fixed 72530 - Merge ExpressionParser & Parser back together

This commit is contained in:
John Camelon 2004-08-30 19:45:33 +00:00
parent 6dc6b84d4b
commit 679764813d
6 changed files with 2848 additions and 3057 deletions

View file

@ -1,90 +0,0 @@
package org.eclipse.cdt.core.parser.tests;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
import org.eclipse.cdt.internal.core.parser.IExpressionParser;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
public class ExprEvalTest extends TestCase {
public static Test suite() {
return new TestSuite(ExprEvalTest.class);
}
public ExprEvalTest(String name) {
super(name);
}
public void runTest(String code, int expectedValue) throws Exception {
final NullSourceElementRequestor nullCallback = new NullSourceElementRequestor();
IExpressionParser parser = InternalParserUtil.createExpressionParser(ParserFactory.createScanner( new CodeReader( code.toCharArray() ), new ScannerInfo(), null, ParserLanguage.CPP, nullCallback, new NullLogService(), null ), ParserLanguage.CPP, null );
IASTExpression expression = parser.expression(null,null, null);
assertEquals(expectedValue, expression.evaluateExpression());
}
public void testInteger() throws Exception {
runTest("5;", 5); //$NON-NLS-1$
runTest( "33;", 33 ); //$NON-NLS-1$
}
public void testNot() throws Exception
{
runTest( "!1;", 0 ); //$NON-NLS-1$
runTest( "!0;", 1 ); //$NON-NLS-1$
runTest( "!4;", 0 ); //$NON-NLS-1$
runTest( "!!4;", 1 ); //$NON-NLS-1$
}
public void testMultiplicational() throws Exception
{
runTest( "3 * 4;", 12 ); //$NON-NLS-1$
runTest( "55 * 2;", 110 ); //$NON-NLS-1$
runTest( "4 / 3;", 1 ); //$NON-NLS-1$
runTest( "100/4;", 25 ); //$NON-NLS-1$
runTest( "8 % 2;", 0 ); //$NON-NLS-1$
runTest( "8 % 3;", 2 ); //$NON-NLS-1$
}
public void testAdditive() throws Exception
{
runTest( "4 + 4;", 8 ); //$NON-NLS-1$
runTest( "4 - 4;", 0 ); //$NON-NLS-1$
}
public void testLogicalAnd() throws Exception
{
runTest( "4 && 5;", 1 ); //$NON-NLS-1$
runTest( "0 && 5;", 0 ); //$NON-NLS-1$
runTest( "5 && 0;", 0 ); //$NON-NLS-1$
runTest( "0 && 0;", 0 ); //$NON-NLS-1$
}
public void testLogicalOr() throws Exception
{
runTest( "4 || 5;", 1 ); //$NON-NLS-1$
runTest( "0 || 5;", 1 ); //$NON-NLS-1$
runTest( "5 || 0;", 1 ); //$NON-NLS-1$
runTest( "0 || 0;", 0 ); //$NON-NLS-1$
}
public void testRelational() throws Exception {
runTest("1 < 2;", 1); //$NON-NLS-1$
runTest("2 < 1;", 0); //$NON-NLS-1$
runTest("2 == 1 + 1;", 1); //$NON-NLS-1$
runTest("2 != 1 + 1;", 0); //$NON-NLS-1$
}
public void testBracketed() throws Exception {
runTest("2 * (3 + 4);", 14); //$NON-NLS-1$
}
}

View file

@ -1,36 +0,0 @@
/*******************************************************************************
* Copyright (c) 2003, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser;
import org.eclipse.cdt.core.parser.BacktrackException;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.KeywordSetKey;
import org.eclipse.cdt.core.parser.ast.IASTCompletionNode;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
import org.eclipse.cdt.core.parser.ast.IASTScope;
/**
* @author jcamelon
*/
public interface IExpressionParser {
/**
* Request a parse from a pre-configured parser to parse an expression.
*
* @param key TODO
* @param expression Optional parameter representing an expression object that
* your particular IParserCallback instance would appreciate
* @throws BacktrackException thrown if the Scanner/Stream provided does not yield a valid
* expression
*/
public IASTExpression expression(IASTScope scope, IASTCompletionNode.CompletionKind kind, KeywordSetKey key) throws BacktrackException, EndOfFileException;
}

View file

@ -23,7 +23,11 @@ import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind;
* @author jcamelon
*
*/
public interface IParserData extends IExpressionParser {
public interface IParserData {
public IASTExpression expression(IASTScope scope, CompletionKind kind,
KeywordSetKey key) throws BacktrackException, EndOfFileException;
/**
* @return Returns the astFactory.
*/

View file

@ -14,28 +14,14 @@ import java.io.File;
import java.io.IOException;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserFactoryError;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.extension.ExtensionDialect;
import org.eclipse.cdt.core.parser.extension.IParserExtensionFactory;
/**
* @author jcamelon
*/
public class InternalParserUtil extends ParserFactory {
private static IParserExtensionFactory extensionFactory = new ParserExtensionFactory( ExtensionDialect.GCC );
public static IExpressionParser createExpressionParser( IScanner scanner, ParserLanguage language, IParserLogService log ) throws ParserFactoryError
{
if( scanner == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_SCANNER );
if( language == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_LANGUAGE );
IParserLogService logService = ( log == null ) ? createDefaultLogService() : log;
return new ExpressionParser( scanner, language, logService, extensionFactory.createParserExtension() );
}
/**
* @param finalPath