This commit was generated by cvs2svn to compensate for changes in r4,

which included commits to RCS files with non-trunk default branches.
This commit is contained in:
hns 2000-12-29 17:58:10 +00:00
parent af35ca5581
commit ee13186158
148 changed files with 34934 additions and 0 deletions

View file

@ -0,0 +1,252 @@
// BasicIO.java
// FESI Copyright (c) Jean-Marc Lugrin, 1999
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package FESI.Extensions;
import FESI.Parser.*;
import FESI.AST.*;
import FESI.Interpreter.*;
import FESI.Exceptions.*;
import FESI.Data.*;
import java.io.*;
/**
* Implements basic input/out capabilities for console based (stdin/stdout)
* applications.
* <P>The few functions allow minimal communication with the user (including
* displaying error messages and prompting) in a way portable to windowing
* environments.
*/
public class BasicIO extends Extension implements BasicIOInterface {
// Implement the EcmaScript functions
class GlobalObjectWrite extends BuiltinFunctionObject {
GlobalObjectWrite(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
for (int i = 0; i<arguments.length; i++) {
System.out.print(arguments[i].toString());
}
return ESUndefined.theUndefined;
}
}
class GlobalObjectWriteln extends BuiltinFunctionObject {
GlobalObjectWriteln(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
for (int i = 0; i<arguments.length; i++) {
System.out.print(arguments[i].toString());
}
System.out.println();
return ESUndefined.theUndefined;
}
}
class GlobalObjectAlert extends BuiltinFunctionObject {
GlobalObjectAlert(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
System.err.print("[[ALERT]] ");
for (int i = 0; i<arguments.length; i++) {
System.err.print(arguments[i].toString());
}
System.err.println();
return ESUndefined.theUndefined;
}
}
class GlobalObjectPrompt extends BuiltinFunctionObject {
GlobalObjectPrompt(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 2);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
String defaultValue = "";
String theQuery = "?";
if (arguments.length>0) theQuery = arguments[0].toString();
if (arguments.length>1) defaultValue = arguments[1].toString();
System.out.print(theQuery + " ["+defaultValue+"] ? ");
System.out.flush();
String response = null;
try {
response = (new BufferedReader(new InputStreamReader(System.in))).readLine();
} catch (IOException e) {
// response = null;
}
if (response==null || response.equals("")) response = defaultValue;
return new ESString(response);
}
}
class GlobalObjectConfirm extends BuiltinFunctionObject {
GlobalObjectConfirm(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 2);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
String theQuery = "?";
if (arguments.length>0) theQuery = arguments[0].toString();
System.out.print(theQuery + " [y/n] ? ");
System.out.flush();
String response = null;
try {
response = (new BufferedReader(new InputStreamReader(System.in))).readLine();
} catch (IOException e) {
// response = null;
}
if (response!=null & response.trim().toLowerCase().startsWith("y")) {
return ESBoolean.makeBoolean(true);
}
return ESBoolean.makeBoolean(false);
}
}
class GlobalObjectExit extends BuiltinFunctionObject {
GlobalObjectExit(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
int status = 0;
if (arguments.length>0) {
status=arguments[0].toInt32();
}
System.exit(status);
return null; // Never reached
}
}
class GlobalObjectNoop extends BuiltinFunctionObject {
GlobalObjectNoop(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 0); // 0 = Just some default value
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
return ESUndefined.theUndefined;
}
}
class GlobalObjectLoad extends BuiltinFunctionObject {
GlobalObjectLoad(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1); // 0 = Just some default value
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
String fileName = null;
if (arguments.length>0) fileName = arguments[0].toString();
if (fileName == null) throw new EcmaScriptException("Missing file name for load");
File file = new File(fileName);
ESValue value;
try {
//value = this.evaluator.evaluateLoadFile(file);
value = this.evaluator.evaluateLoadModule(fileName);
} catch (EcmaScriptParseException e) {
e.setNeverIncomplete();
throw e;
}
return value;
}
}
// Instance variables
private Evaluator evaluator = null;
private ESObject document = null;
private ESObject window = null;
/**
* Create a new instance of the BasicIO extension
*/
public BasicIO () {
super();
}
// implements BasicIOInterface
public ESObject getDocument() { return document; }
/**
* Initialize the extension
*/
public void initializeExtension(Evaluator evaluator) throws EcmaScriptException {
this.evaluator = evaluator;
GlobalObject go = evaluator.getGlobalObject();
document = ObjectObject.createObject(evaluator);
window = ObjectObject.createObject(evaluator);
FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype();
go.putHiddenProperty("document", document);
document.putHiddenProperty("write",
new GlobalObjectWrite("write", evaluator, fp));
document.putHiddenProperty("writeln",
new GlobalObjectWriteln("writeln", evaluator, fp));
document.putHiddenProperty("open",
new GlobalObjectNoop("open", evaluator, fp));
document.putHiddenProperty("close",
new GlobalObjectNoop("close", evaluator, fp));
document.putHiddenProperty("URL", new ESString("file://<unknown>"));
go.putHiddenProperty("window", window);
window.putHiddenProperty("alert",
new GlobalObjectAlert("alert", evaluator, fp));
window.putHiddenProperty("prompt",
new GlobalObjectPrompt("prompt", evaluator, fp));
window.putHiddenProperty("confirm",
new GlobalObjectConfirm("confirm", evaluator, fp));
go.putHiddenProperty("write",
new GlobalObjectWrite("write", evaluator, fp));
go.putHiddenProperty("writeln",
new GlobalObjectWriteln("writeln", evaluator, fp));
go.putHiddenProperty("alert",
new GlobalObjectAlert("alert", evaluator, fp));
go.putHiddenProperty("prompt",
new GlobalObjectPrompt("prompt", evaluator, fp));
go.putHiddenProperty("confirm",
new GlobalObjectConfirm("confirm", evaluator, fp));
go.putHiddenProperty("exit",
new GlobalObjectExit("exit", evaluator, fp));
go.putHiddenProperty("load",
new GlobalObjectLoad("load", evaluator, fp));
}
}

View file

@ -0,0 +1,35 @@
// BasicIOInterface.java
// FESI Copyright (c) Jean-Marc Lugrin, 1999
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package FESI.Extensions;
import FESI.Data.ESObject;
/**
* The interface of all BasicIO extensions, used bu the interpreter
* to access the document object.
*/
public interface BasicIOInterface {
/**
* Return an object with the document routines (for compatibility with
* Netscape). This object will be the "document" object and the caller
* may add its URL property.
*/
abstract public ESObject getDocument();
}

View file

@ -0,0 +1,224 @@
// BasicIOs.java
// FESI Copyright (c) Jean-Marc Lugrin, 1999
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package FESI.Extensions;
import FESI.Parser.*;
import FESI.AST.*;
import FESI.Data.*;
import FESI.Interpreter.*;
import FESI.Exceptions.*;
import FESI.gui.*;
import FESI.swinggui.*;
import java.io.*;
/**
* Swing based basic IO for FESI - See BasicIO
*/
public class BasicIOs extends Extension implements BasicIOInterface {
class GlobalObjectWrite extends BuiltinFunctionObject {
GlobalObjectWrite(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
for (int i = 0; i<arguments.length; i++) {
System.out.print(arguments[i].toString());
}
return ESUndefined.theUndefined;
}
}
class GlobalObjectWriteln extends BuiltinFunctionObject {
GlobalObjectWriteln(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
for (int i = 0; i<arguments.length; i++) {
System.out.print(arguments[i].toString());
}
System.out.println();
return ESUndefined.theUndefined;
}
}
class GlobalObjectAlert extends BuiltinFunctionObject {
GlobalObjectAlert(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
StringBuffer sb = new StringBuffer();
for (int i = 0; i<arguments.length; i++) {
sb.append(arguments[i].toString());
}
MessageBox mb = new SwingMessageBox("EcmaScript Alert",sb.toString());
mb.waitOK();
return ESUndefined.theUndefined;
}
}
class GlobalObjectPrompt extends BuiltinFunctionObject {
GlobalObjectPrompt(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
String prompt = "";
String defaultResponse = "";
if (arguments.length>0) prompt = arguments[0].toString();
if (arguments.length>1) defaultResponse = arguments[1].toString();
PromptBox pb = new SwingPromptBox("EcmaScript promt",prompt, defaultResponse);
return new ESString(pb.waitResponse());
}
}
class GlobalObjectConfirm extends BuiltinFunctionObject {
GlobalObjectConfirm(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
StringBuffer sb = new StringBuffer();
for (int i = 0; i<arguments.length; i++) {
sb.append(arguments[i].toString());
}
ConfirmationBox mb = new SwingConfirmationBox("EcmaScript Confirm",sb.toString());
boolean response = mb.waitYesOrNo();
return ESBoolean.makeBoolean(response);
}
}
class GlobalObjectExit extends BuiltinFunctionObject {
GlobalObjectExit(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
int status = 0;
if (arguments.length>0) {
status=arguments[0].toInt32();
}
System.exit(status);
return null; // Never reached
}
}
class GlobalObjectNoop extends BuiltinFunctionObject {
GlobalObjectNoop(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 0); // 0 = Just some default value
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
return ESUndefined.theUndefined;
}
}
class GlobalObjectLoad extends BuiltinFunctionObject {
GlobalObjectLoad(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1); // 0 = Just some default value
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
String fileName = null;
if (arguments.length>0) fileName = arguments[0].toString();
if (fileName == null) throw new EcmaScriptException("Missing file name for load");
File file = new File(fileName);
ESValue value;
try {
//value = this.evaluator.evaluateLoadFile(file);
value = this.evaluator.evaluateLoadModule(fileName);
} catch (EcmaScriptParseException e) {
e.setNeverIncomplete();
throw e;
}
return value;
}
}
private Evaluator evaluator = null;
private ESObject document = null;
private ESObject window = null;
public BasicIOs () {
super();
}
// implements BasicIOInterface
public ESObject getDocument() { return document; }
public void initializeExtension(Evaluator evaluator) throws EcmaScriptException {
this.evaluator = evaluator;
GlobalObject go = evaluator.getGlobalObject();
document = ObjectObject.createObject(evaluator);
window = ObjectObject.createObject(evaluator);
FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype();
go.putHiddenProperty("document", document);
document.putHiddenProperty("write",
new GlobalObjectWrite("write", evaluator, fp));
document.putHiddenProperty("writeln",
new GlobalObjectWriteln("writeln", evaluator, fp));
document.putHiddenProperty("open",
new GlobalObjectNoop("open", evaluator, fp));
document.putHiddenProperty("close",
new GlobalObjectNoop("close", evaluator, fp));
document.putHiddenProperty("URL", new ESString("file://<unknown>"));
go.putHiddenProperty("window", window);
window.putHiddenProperty("alert",
new GlobalObjectAlert("alert", evaluator, fp));
window.putHiddenProperty("prompt",
new GlobalObjectPrompt("prompt", evaluator, fp));
window.putHiddenProperty("confirm",
new GlobalObjectConfirm("confirm", evaluator, fp));
go.putHiddenProperty("write",
new GlobalObjectWrite("write", evaluator, fp));
go.putHiddenProperty("writeln",
new GlobalObjectWriteln("writeln", evaluator, fp));
go.putHiddenProperty("alert",
new GlobalObjectAlert("alert", evaluator, fp));
go.putHiddenProperty("prompt",
new GlobalObjectPrompt("prompt", evaluator, fp));
go.putHiddenProperty("confirm",
new GlobalObjectConfirm("confirm", evaluator, fp));
go.putHiddenProperty("exit",
new GlobalObjectExit("exit", evaluator, fp));
go.putHiddenProperty("load",
new GlobalObjectLoad("load", evaluator, fp));
}
}

View file

@ -0,0 +1,224 @@
// BasicIOw.java
// FESI Copyright (c) Jean-Marc Lugrin, 1999
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package FESI.Extensions;
import FESI.Parser.*;
import FESI.AST.*;
import FESI.Data.*;
import FESI.Interpreter.*;
import FESI.Exceptions.*;
import FESI.gui.*;
import FESI.awtgui.*;
import java.io.*;
/**
* Swing based basic IO for FESI - see BasicIO
*/
public class BasicIOw extends Extension implements BasicIOInterface {
class GlobalObjectWrite extends BuiltinFunctionObject {
GlobalObjectWrite(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
for (int i = 0; i<arguments.length; i++) {
System.out.print(arguments[i].toString());
}
return ESUndefined.theUndefined;
}
}
class GlobalObjectWriteln extends BuiltinFunctionObject {
GlobalObjectWriteln(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
for (int i = 0; i<arguments.length; i++) {
System.out.print(arguments[i].toString());
}
System.out.println();
return ESUndefined.theUndefined;
}
}
class GlobalObjectAlert extends BuiltinFunctionObject {
GlobalObjectAlert(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
StringBuffer sb = new StringBuffer();
for (int i = 0; i<arguments.length; i++) {
sb.append(arguments[i].toString());
}
MessageBox mb = new AwtMessageBox("EcmaScript Alert",sb.toString());
mb.waitOK();
return ESUndefined.theUndefined;
}
}
class GlobalObjectPrompt extends BuiltinFunctionObject {
GlobalObjectPrompt(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
String prompt = "";
String defaultResponse = "";
if (arguments.length>0) prompt = arguments[0].toString();
if (arguments.length>1) defaultResponse = arguments[1].toString();
PromptBox pb = new AwtPromptBox("EcmaScript promt",prompt, defaultResponse);
return new ESString(pb.waitResponse());
}
}
class GlobalObjectConfirm extends BuiltinFunctionObject {
GlobalObjectConfirm(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
StringBuffer sb = new StringBuffer();
for (int i = 0; i<arguments.length; i++) {
sb.append(arguments[i].toString());
}
ConfirmationBox mb = new AwtConfirmationBox("EcmaScript Confirm",sb.toString());
boolean response = mb.waitYesOrNo();
return ESBoolean.makeBoolean(response);
}
}
class GlobalObjectExit extends BuiltinFunctionObject {
GlobalObjectExit(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
int status = 0;
if (arguments.length>0) {
status=arguments[0].toInt32();
}
System.exit(status);
return null; // Never reached
}
}
class GlobalObjectNoop extends BuiltinFunctionObject {
GlobalObjectNoop(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 0); // 0 = Just some default value
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
return ESUndefined.theUndefined;
}
}
class GlobalObjectLoad extends BuiltinFunctionObject {
GlobalObjectLoad(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1); // 0 = Just some default value
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
String fileName = null;
if (arguments.length>0) fileName = arguments[0].toString();
if (fileName == null) throw new EcmaScriptException("Missing file name for load");
File file = new File(fileName);
ESValue value ;
try {
//value = this.evaluator.evaluateLoadFile(file);
value = this.evaluator.evaluateLoadModule(fileName);
} catch (EcmaScriptParseException e) {
e.setNeverIncomplete();
throw e;
}
return value;
}
}
private Evaluator evaluator = null;
private ESObject document = null;
private ESObject window = null;
public BasicIOw () {
super();
}
// implements BasicIOInterface
public ESObject getDocument() { return document; }
public void initializeExtension(Evaluator evaluator) throws EcmaScriptException {
this.evaluator = evaluator;
GlobalObject go = evaluator.getGlobalObject();
document = ObjectObject.createObject(evaluator);
window = ObjectObject.createObject(evaluator);
FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype();
go.putHiddenProperty("document", document);
document.putHiddenProperty("write",
new GlobalObjectWrite("write", evaluator, fp));
document.putHiddenProperty("writeln",
new GlobalObjectWriteln("writeln", evaluator, fp));
document.putHiddenProperty("open",
new GlobalObjectNoop("open", evaluator, fp));
document.putHiddenProperty("close",
new GlobalObjectNoop("close", evaluator, fp));
document.putHiddenProperty("URL", new ESString("file://<unknown>"));
go.putHiddenProperty("window", window);
window.putHiddenProperty("alert",
new GlobalObjectAlert("alert", evaluator, fp));
window.putHiddenProperty("prompt",
new GlobalObjectPrompt("prompt", evaluator, fp));
window.putHiddenProperty("confirm",
new GlobalObjectConfirm("confirm", evaluator, fp));
go.putHiddenProperty("write",
new GlobalObjectWrite("write", evaluator, fp));
go.putHiddenProperty("writeln",
new GlobalObjectWriteln("writeln", evaluator, fp));
go.putHiddenProperty("alert",
new GlobalObjectAlert("alert", evaluator, fp));
go.putHiddenProperty("prompt",
new GlobalObjectPrompt("prompt", evaluator, fp));
go.putHiddenProperty("confirm",
new GlobalObjectConfirm("confirm", evaluator, fp));
go.putHiddenProperty("exit",
new GlobalObjectExit("exit", evaluator, fp));
go.putHiddenProperty("load",
new GlobalObjectLoad("load", evaluator, fp));
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,26 @@
// Extension.java
// FESI Copyright (c) Jean-Marc Lugrin, 1999
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package FESI.Extensions;
import FESI.Interpreter.*;
import FESI.Exceptions.*;
public abstract class Extension {
abstract public void initializeExtension(Evaluator evaluator)
throws EcmaScriptException;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,567 @@
// GNURegExp.java
// FESI Copyright (c) Jean-Marc Lugrin, 1999
// this file (c) mike dillon, 1999
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package FESI.Extensions;
import FESI.Parser.*;
import FESI.AST.*;
import FESI.Interpreter.*;
import FESI.Exceptions.*;
import FESI.Data.*;
import java.util.Vector;
import gnu.regexp.*;
/**
* An EcmaScript RegExp object based on GNU pattern matcher.
* May not coexist with the ORO regexp matcher.
*/
class ESGNURegExp extends ESObject
{
private String regExpString;
private boolean ignoreCase = false;
private boolean global = false;
private RE pattern = null; // null means no valid pattern
private int groups;
static private final String IGNORECASEstring = "ignoreCase";
static private final int IGNORECASEhash = IGNORECASEstring.hashCode();
static private final String GLOBALstring = "global";
static private final int GLOBALhash = GLOBALstring.hashCode();
// Normal constructor
ESGNURegExp(ESObject prototype, Evaluator evaluator, String regExpString)
{
super(prototype, evaluator);
this.regExpString = regExpString;
}
// Prototype constructor
ESGNURegExp(ESObject prototype, Evaluator evaluator)
{
super(prototype, evaluator);
this.regExpString = "";
}
public RE getPattern() throws EcmaScriptException
{
if (pattern == null)
{
compile();
}
return pattern;
}
public boolean isGlobal()
{
return global;
}
public void compile() throws EcmaScriptException
{
try
{
pattern = new RE(regExpString,
(ignoreCase ? RE.REG_ICASE : 0) | RE.REG_MULTILINE,
RESyntax.RE_SYNTAX_PERL5);
}
catch(REException e)
{
throw new EcmaScriptException(this.toString(), e);
}
}
public String getESClassName()
{
return "RegExp";
}
public String toString()
{
return "/" + ((regExpString == null) ? "<null>" : regExpString)
+ "/";
}
public String toDetailString()
{
return "ES:[Object: builtin " + this.getClass().getName() + ":"
+ this.toString() + "]";
}
public ESValue getPropertyInScope(String propertyName,
ScopeChain previousScope, int hash) throws EcmaScriptException
{
if (IGNORECASEstring.equals(propertyName))
{
return ESBoolean.makeBoolean(ignoreCase);
}
else if (GLOBALstring.equals(propertyName))
{
return ESBoolean.makeBoolean(global);
}
else
{
return super.getPropertyInScope(propertyName,
previousScope, hash);
}
}
public ESValue getProperty(String propertyName, int hash)
throws EcmaScriptException
{
if (IGNORECASEstring.equals(propertyName))
{
return ESBoolean.makeBoolean(ignoreCase);
}
else if (GLOBALstring.equals(propertyName))
{
return ESBoolean.makeBoolean(global);
}
else
{
return super.getProperty(propertyName, hash);
}
}
public void putProperty(String propertyName, ESValue propertyValue,
int hash) throws EcmaScriptException
{
if (hash == IGNORECASEhash &&
IGNORECASEstring.equals(propertyName))
{
boolean oldIgnoreCase = ignoreCase;
ignoreCase = (((ESPrimitive) propertyValue).booleanValue());
if (oldIgnoreCase != ignoreCase)
pattern = null; // force recompilation
}
else if (hash == GLOBALhash && GLOBALstring.equals(propertyName))
{
global = (((ESPrimitive) propertyValue).booleanValue());
}
else
{
super.putProperty(propertyName, propertyValue, hash);
}
}
public String[] getSpecialPropertyNames()
{
String [] ns = { GLOBALstring, IGNORECASEstring };
return ns;
}
}
public class GNURegExp extends Extension
{
static private final String INDEXstring = "index";
static private final int INDEXhash = INDEXstring.hashCode();
static private final String INPUTstring = "input";
static private final int INPUThash = INPUTstring.hashCode();
private Evaluator evaluator = null;
private ESObject esRegExpPrototype;
class ESRegExpPrototypeTest extends BuiltinFunctionObject
{
ESRegExpPrototypeTest(String name, Evaluator evaluator,
FunctionPrototype fp)
{
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments) throws EcmaScriptException
{
if (arguments.length < 1)
{
throw new EcmaScriptException(
"test requires 1 string argument");
}
RE pattern = ((ESGNURegExp) thisObject).getPattern();
boolean contains = pattern.getMatch(arguments[0].toString())
!= null;
return ESBoolean.makeBoolean(contains);
}
}
class ESRegExpPrototypeExec extends BuiltinFunctionObject
{
ESRegExpPrototypeExec(String name, Evaluator evaluator,
FunctionPrototype fp)
{
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments) throws EcmaScriptException
{
if (arguments.length < 1)
{
throw new EcmaScriptException(
"exec requires 1 string argument");
}
RE pattern = ((ESGNURegExp) thisObject).getPattern();
String str = arguments[0].toString();
REMatch match = pattern.getMatch(str);
if (match != null)
{
int groups = pattern.getNumSubs() + 1;
ESObject ap = this.evaluator.getArrayPrototype();
ArrayPrototype resultArray = new ArrayPrototype(ap,
this.evaluator);
resultArray.setSize(groups);
resultArray.putProperty(INDEXstring, new ESNumber(
match.getStartIndex()), INDEXhash);
resultArray.putProperty(INPUTstring, new ESString(str), INPUThash);
for (int i = 0; i < groups; i++)
{
String sub = match.toString(i);
resultArray.setElementAt(new ESString(
(sub == null) ? "" : sub), i);
}
return resultArray;
}
else
{
return ESNull.theNull;
}
}
}
class GlobalObjectRegExp extends BuiltinFunctionObject
{
GlobalObjectRegExp(String name, Evaluator evaluator,
FunctionPrototype fp)
{
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments) throws EcmaScriptException
{
return doConstruct(thisObject, arguments);
}
public ESObject doConstruct(ESObject thisObject,
ESValue[] arguments) throws EcmaScriptException
{
ESGNURegExp regExp = null;
if (arguments.length == 0)
{
throw new EcmaScriptException(
"GNURegExp requires 1 argument");
}
else if (arguments.length == 1)
{
regExp = new ESGNURegExp(esRegExpPrototype,
this.evaluator, arguments[0].toString());
}
return regExp;
}
}
public void initializeExtension(Evaluator evaluator)
throws EcmaScriptException
{
this.evaluator = evaluator;
GlobalObject go = evaluator.getGlobalObject();
ObjectPrototype op = (ObjectPrototype) evaluator.getObjectPrototype();
FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype();
esRegExpPrototype = new ESGNURegExp(op, evaluator);
ESObject globalObjectRegExp = new GlobalObjectRegExp("RegExp", evaluator, fp);
globalObjectRegExp.putHiddenProperty("prototype",esRegExpPrototype);
globalObjectRegExp.putHiddenProperty("length",new ESNumber(1));
esRegExpPrototype.putHiddenProperty("constructor",globalObjectRegExp);
esRegExpPrototype.putHiddenProperty("test",
new ESRegExpPrototypeTest("test", evaluator, fp));
esRegExpPrototype.putHiddenProperty("exec",
new ESRegExpPrototypeExec("exec", evaluator, fp));
go.putHiddenProperty("RegExp", globalObjectRegExp);
class StringPrototypeSearch extends BuiltinFunctionObject
{
StringPrototypeSearch(String name, Evaluator evaluator,
FunctionPrototype fp)
{
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments) throws EcmaScriptException
{
if (arguments.length < 1)
{
throw new EcmaScriptException(
"search requires 1 pattern argument");
}
String str = thisObject.toString();
ESGNURegExp pattern;
if (arguments[0] instanceof ESGNURegExp)
{
pattern = (ESGNURegExp) arguments[0];
}
else
{
throw new EcmaScriptException("The search argument must be a GNURegExp");
}
RE re = pattern.getPattern();
REMatch match = re.getMatch(str);
int matchStart = (match == null) ? -1 : match
.getStartIndex();
return new ESNumber(matchStart);
}
}
class StringPrototypeReplace extends BuiltinFunctionObject
{
StringPrototypeReplace(String name, Evaluator evaluator,
FunctionPrototype fp)
{
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments) throws EcmaScriptException
{
if (arguments.length < 2)
{
throw new EcmaScriptException(
"replace requires 2 arguments: pattern and replacement string");
}
String str = thisObject.toString();
ESGNURegExp pattern;
if (arguments[0] instanceof ESGNURegExp)
{
pattern = (ESGNURegExp) arguments[0];
}
else
{
throw new EcmaScriptException(
"The replace argument must be a GNURegExp");
}
String replacement = arguments[1].toString();
RE re = pattern.getPattern();
String result = null;
if (pattern.isGlobal())
{
result = re.substituteAll(str,
replacement);
}
else
{
result = re.substitute(str,
replacement);
}
return new ESString(result);
}
}
class StringPrototypeMatch extends BuiltinFunctionObject
{
StringPrototypeMatch(String name, Evaluator evaluator,
FunctionPrototype fp)
{
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments) throws EcmaScriptException
{
if (arguments.length < 1)
{
throw new EcmaScriptException(
"match requires 1 pattern argument");
}
String str = thisObject.toString();
ESGNURegExp pattern;
if (arguments[0] instanceof ESGNURegExp)
{
pattern = (ESGNURegExp) arguments[0];
}
else
{
throw new EcmaScriptException(
"The match argument must be a GNURegExp");
}
RE re = pattern.getPattern();
REMatch match = re.getMatch(str);
if (match != null)
{
// Group count is one more than the
// subexpression count
int groups = re.getNumSubs() + 1;
ESObject ap = this.evaluator
.getArrayPrototype();
ArrayPrototype resultArray = new
ArrayPrototype(ap, this.evaluator);
resultArray.setSize(groups);
resultArray.putProperty(INDEXstring,
new ESNumber(match.getStartIndex()),
INDEXhash);
resultArray.putProperty(INPUTstring,
new ESString(str), INPUThash);
for (int i = 0; i < groups; i++)
{
String sub = match.toString(i);
if (sub != null)
{
resultArray.setElementAt(
new ESString(sub), i);
}
else
{
resultArray.setElementAt(
new ESString(""), i);
}
}
return resultArray;
}
else
{
return ESNull.theNull;
}
}
}
class StringPrototypeSplit extends BuiltinFunctionObject
{
StringPrototypeSplit(String name, Evaluator evaluator,
FunctionPrototype fp)
{
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments) throws EcmaScriptException
{
String str = thisObject.toString();
ESObject ap = this.evaluator.getArrayPrototype();
ArrayPrototype theArray = new ArrayPrototype(ap,
this.evaluator);
if (arguments.length <= 0)
{
theArray.setSize(1);
theArray.setElementAt(thisObject, 0);
}
else
{
if (arguments[0] instanceof ESGNURegExp)
{
ESGNURegExp pattern = (ESGNURegExp)
arguments[0];
int n = -1;
if (arguments.length > 1)
{
n = arguments[1].toUInt32();
if (n <= 0) n = -1;
}
RE re = pattern.getPattern();
Vector result = new Vector();
int pos = 0;
int len = str.length();
while (pos < len)
{
REMatch match = re.getMatch(str, pos);
if (match != null &&
(n == -1 || n - 1 > result.size()))
{
int start = match.getStartIndex();
int end = match.getEndIndex();
int matchLen = end - start;
int chunkLen = start - pos;
if (matchLen == 0) chunkLen++;
result.addElement(str.substring(pos,
pos + chunkLen));
pos = (int)Math.max(end, pos + 1);
}
else
{
result.addElement(str.substring(pos));
break;
}
}
int l = result.size();
theArray.setSize(l);
for (int i = 0; i < l; i++)
{
theArray.setElementAt(new ESString(
(String)result.elementAt(i)), i);
}
}
else
{ // ! instanceof ESGNURegExp
String sep = arguments[0].toString();
int strLen = str.length();
int sepLen = sep.length();
if (sepLen == 0)
{
theArray.setSize(strLen);
for (int i = 0; i < strLen; i++)
{
theArray.setElementAt(new ESString(
str.substring(i, i + 1)), i);
}
}
else
{
int i = 0;
int start = 0;
while (start < strLen)
{
int pos = str.indexOf(sep, start);
if (pos < 0)
pos = strLen;
theArray.setSize(i + 1);
theArray.setElementAt(new
ESString(str.substring(start, pos)), i);
start = pos + sepLen;
i++;
}
}
} // instanceof ESGNURegExp
}
return theArray;
}
}
ESObject stringPrototype = evaluator.getStringPrototype();
stringPrototype.putHiddenProperty("search",
new StringPrototypeSearch("search", evaluator, fp));
stringPrototype.putHiddenProperty("replace",
new StringPrototypeReplace("replace", evaluator, fp));
stringPrototype.putHiddenProperty("match",
new StringPrototypeMatch("match", evaluator, fp));
stringPrototype.putHiddenProperty("split",
new StringPrototypeSplit("split", evaluator, fp));
}
}

View file

@ -0,0 +1,99 @@
// JavaAccess.java
// FESI Copyright (c) Jean-Marc Lugrin, 1999
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package FESI.Extensions;
import FESI.Parser.*;
import FESI.AST.*;
import FESI.Data.*;
import FESI.Interpreter.*;
import FESI.Exceptions.*;
import java.awt.event.*;
import java.util.EventListener;
public class JavaAccess extends Extension {
class GlobalObjectJavaTypeOf extends BuiltinFunctionObject {
GlobalObjectJavaTypeOf(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
if (arguments.length>0) {
Object obj = arguments[0].toJavaObject();
String cn = (obj==null) ? "null" : ESLoader.typeName(obj.getClass());
return new ESString(cn);
}
return ESUndefined.theUndefined;
}
}
class GlobalObjectLoadExtension extends BuiltinFunctionObject {
GlobalObjectLoadExtension(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
Object ext = null;
if (arguments.length>0) {
String pathName = arguments[0].toString();
ext = this.evaluator.addExtension(pathName);
}
return ESBoolean.makeBoolean(ext!=null);
}
}
private Evaluator evaluator = null;
public JavaAccess () {
super();
}
public void initializeExtension(Evaluator evaluator) throws EcmaScriptException {
this.evaluator = evaluator;
GlobalObject go = evaluator.getGlobalObject();
FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype();
go.putHiddenProperty("javaTypeOf",
new GlobalObjectJavaTypeOf("javaTypeOf", evaluator, fp));
go.putHiddenProperty("loadExtension",
new GlobalObjectLoadExtension("loadExtension", evaluator, fp));
ESPackages packagesObject = (ESPackages) evaluator.getPackageObject();
String java = ("java").intern();
ESPackages javaPackages = (ESPackages) packagesObject.getProperty(java,java.hashCode());
go.putHiddenProperty("Packages", packagesObject);
go.putHiddenProperty(java, javaPackages);
ESBeans javaBeans = new ESBeans(evaluator);
go.putHiddenProperty("Beans", javaBeans);
}
}

View file

@ -0,0 +1,437 @@
// ORORegExp.java
// FESI Copyright (c) Jean-Marc Lugrin, 1999
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package FESI.Extensions;
import FESI.Parser.*;
import FESI.AST.*;
import FESI.Interpreter.*;
import FESI.Exceptions.*;
import FESI.Data.*;
import java.util.Vector;
import com.oroinc.text.regex.*;
/**
* An EcmaScript RegExp object based on OROInc pattern matcher.
* May not coexist with the GNU regexp matcher.
*/
class ESORORegExp extends ESObject {
private String regExpString;
private boolean ignoreCase = false;
private boolean global = false;
private Pattern pattern = null; // null means no valid pattern
private int groups;
private PatternCompiler compiler;
private PatternMatcher matcher;
static private final String IGNORECASEstring = ("ignoreCase").intern();
static private final int IGNORECASEhash = IGNORECASEstring.hashCode();
static private final String GLOBALstring = ("global").intern();
static private final int GLOBALhash = GLOBALstring.hashCode();
// Normal constructor
ESORORegExp(ESObject prototype, Evaluator evaluator,
PatternCompiler compiler, PatternMatcher matcher,
String regExpString) {
super(prototype, evaluator);
this.compiler = compiler;
this.matcher = matcher;
this.regExpString = regExpString;
}
// Prototype constructor
ESORORegExp(ESObject prototype, Evaluator evaluator,
PatternCompiler compiler, PatternMatcher matcher) {
super(prototype, evaluator);
this.compiler = compiler;
this.matcher = matcher;
this.regExpString = "";
}
public Pattern getPattern() throws EcmaScriptException {
if (pattern == null) {
compile();
}
return pattern;
}
public boolean isGlobal() {
return global;
}
public void compile() throws EcmaScriptException {
// Recompile the pattern
try {
pattern = compiler.compile(regExpString,
ignoreCase ? Perl5Compiler.CASE_INSENSITIVE_MASK : Perl5Compiler.DEFAULT_MASK);
} catch(MalformedPatternException e) {
throw new EcmaScriptException("MalformedPatternException: /" +
regExpString + "/", e);
}
}
public String getESClassName() {
return "RegExp";
}
public String toString() {
if (regExpString==null) return "/<null>/";
return "/"+regExpString+"/";
}
public String toDetailString() {
return "ES:[Object: builtin " + this.getClass().getName() + ":" +
this.toString() + "]";
}
public ESValue getPropertyInScope(String propertyName, ScopeChain previousScope, int hash)
throws EcmaScriptException {
if (propertyName.equals(IGNORECASEstring)) {
return ESBoolean.makeBoolean(ignoreCase);
} else if (propertyName.equals(GLOBALstring)) {
return ESBoolean.makeBoolean(global);
}
return super.getPropertyInScope(propertyName, previousScope, hash);
}
public ESValue getProperty(String propertyName, int hash)
throws EcmaScriptException {
if (propertyName.equals(IGNORECASEstring)) {
return ESBoolean.makeBoolean(ignoreCase);
} else if (propertyName.equals(GLOBALstring)) {
return ESBoolean.makeBoolean(global);
} else {
return super.getProperty(propertyName, hash);
}
}
public void putProperty(String propertyName, ESValue propertyValue, int hash)
throws EcmaScriptException {
if (hash==IGNORECASEhash && propertyName.equals(IGNORECASEstring)) {
boolean oldIgnoreCase = ignoreCase;
ignoreCase = (((ESPrimitive) propertyValue).booleanValue());
if (oldIgnoreCase!=ignoreCase) pattern = null; // force recompilation
} else if (hash==GLOBALhash && propertyName.equals(GLOBALstring)) {
global = (((ESPrimitive) propertyValue).booleanValue());
} else {
super.putProperty(propertyName, propertyValue, hash);
}
}
public String[] getSpecialPropertyNames() {
String [] ns = {GLOBALstring, IGNORECASEstring};
return ns;
}
}
public class ORORegExp extends Extension {
static private final String INDEXstring = ("index").intern();
static private final int INDEXhash = INDEXstring.hashCode();
static private final String INPUTstring = ("input").intern();
static private final int INPUThash = INPUTstring.hashCode();
private Evaluator evaluator = null;
private ESObject esRegExpPrototype;
private PatternCompiler compiler;
private PatternMatcher matcher;
class ESRegExpPrototypeTest extends BuiltinFunctionObject {
ESRegExpPrototypeTest(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
if (arguments.length<1) {
throw new EcmaScriptException("test requires 1 string argument");
}
ESORORegExp pattern = (ESORORegExp) thisObject;
String str = arguments[0].toString();
PatternMatcherInput input = new PatternMatcherInput(str);
return ESBoolean.makeBoolean(matcher.contains(input, pattern.getPattern()));
}
}
class ESRegExpPrototypeExec extends BuiltinFunctionObject {
ESRegExpPrototypeExec(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
if (arguments.length<1) {
throw new EcmaScriptException("exec requires 1 string argument");
}
ESORORegExp pattern = (ESORORegExp) thisObject;
String str = arguments[0].toString();
PatternMatcherInput input = new PatternMatcherInput(str);
if (matcher.contains(input, pattern.getPattern())) {
MatchResult result = matcher.getMatch();
int groups = result.groups();
ESObject ap = this.evaluator.getArrayPrototype();
ArrayPrototype resultArray = new ArrayPrototype(ap, this.evaluator);
resultArray.setSize(groups);
resultArray.putProperty(INDEXstring,
new ESNumber(result.beginOffset(0)), INDEXhash);
resultArray.putProperty(INPUTstring,
new ESString(str), INPUThash);
for (int i = 0; i<groups; i++) {
int beg = result.beginOffset(i);
int end = result.endOffset(i);
if (beg<0 || end < 0) {
resultArray.setElementAt(new ESString(""),i);
} else {
resultArray.setElementAt(new ESString(str.substring(beg,end)),i);
}
}
return resultArray;
} else {
return ESNull.theNull;
}
}
}
class GlobalObjectRegExp extends BuiltinFunctionObject {
GlobalObjectRegExp(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
return doConstruct(thisObject, arguments);
}
public ESObject doConstruct(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
ESORORegExp regExp = null;
if (arguments.length==0) {
throw new EcmaScriptException("RegExp requires 1 or 2 arguments");
} else if (arguments.length==1) {
regExp = new ESORORegExp(esRegExpPrototype, this.evaluator, compiler, matcher, arguments[0].toString());
//} else if (arguments.length>1) {
// file = new ESORORegExp(esFilePrototype, this.evaluator, arguments[0].toString(),arguments[1].toString());
}
return regExp;
}
}
public ORORegExp () {
super();
}
public void initializeExtension(Evaluator evaluator) throws EcmaScriptException {
// Create Perl5Compiler and Perl5Matcher instances.
compiler = new Perl5Compiler();
matcher = new Perl5Matcher();
this.evaluator = evaluator;
GlobalObject go = evaluator.getGlobalObject();
ObjectPrototype op = (ObjectPrototype) evaluator.getObjectPrototype();
FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype();
esRegExpPrototype = new ESORORegExp(op, evaluator, compiler, matcher);
ESObject globalObjectRegExp =
new GlobalObjectRegExp("RegExp", evaluator, fp);
globalObjectRegExp.putHiddenProperty("prototype",esRegExpPrototype);
globalObjectRegExp.putHiddenProperty("length",new ESNumber(1));
esRegExpPrototype.putHiddenProperty("constructor",globalObjectRegExp);
esRegExpPrototype.putHiddenProperty("test",
new ESRegExpPrototypeTest("test", evaluator, fp));
esRegExpPrototype.putHiddenProperty("exec",
new ESRegExpPrototypeExec("exec", evaluator, fp));
go.putHiddenProperty("RegExp", globalObjectRegExp);
class StringPrototypeSearch extends BuiltinFunctionObject {
StringPrototypeSearch(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
if (arguments.length<1) {
throw new EcmaScriptException("search requires 1 pattern argument");
}
String str = thisObject.toString();
PatternMatcherInput input = new PatternMatcherInput(str);
ESORORegExp pattern;
if (arguments[0] instanceof ESORORegExp) {
pattern = (ESORORegExp) arguments[0];
} else {
throw new EcmaScriptException("The search argument must be a RegExp");
}
if(matcher.contains(input, pattern.getPattern())){
MatchResult result = matcher.getMatch();
return new ESNumber(result.beginOffset(0));
} else {
return new ESNumber(-1);
}
}
}
class StringPrototypeReplace extends BuiltinFunctionObject {
StringPrototypeReplace(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
if (arguments.length<2) {
throw new EcmaScriptException("replace requires 2 arguments: pattern and replacement string");
}
String str = thisObject.toString();
ESORORegExp pattern;
if (arguments[0] instanceof ESORORegExp) {
pattern = (ESORORegExp) arguments[0];
} else {
throw new EcmaScriptException("The replace argument must be a RegExp");
}
String replacement = arguments[1].toString();
// USE DEPRECATED ROUTINE BECAUSE I AM LAZY
String result = Util.substitute(matcher, pattern.getPattern(), replacement, str,
pattern.isGlobal() ? Util.SUBSTITUTE_ALL : 1, Util.INTERPOLATE_ALL);
return new ESString(result);
}
}
class StringPrototypeMatch extends BuiltinFunctionObject {
StringPrototypeMatch(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
if (arguments.length<1) {
throw new EcmaScriptException("match requires 1 pattern argument");
}
String str = thisObject.toString();
ESORORegExp pattern;
if (arguments[0] instanceof ESORORegExp) {
pattern = (ESORORegExp) arguments[0];
} else {
throw new EcmaScriptException("The match argument must be a RegExp");
}
PatternMatcherInput input = new PatternMatcherInput(str);
if (matcher.contains(input, pattern.getPattern())) {
MatchResult result = matcher.getMatch();
int groups = result.groups();
ESObject ap = this.evaluator.getArrayPrototype();
ArrayPrototype resultArray = new ArrayPrototype(ap, this.evaluator);
resultArray.setSize(groups);
resultArray.putProperty(INDEXstring,
new ESNumber(result.beginOffset(0)), INDEXhash);
resultArray.putProperty(INPUTstring,
new ESString(str), INPUThash);
for (int i = 0; i<groups; i++) {
int beg = result.beginOffset(i);
int end = result.endOffset(i);
resultArray.setElementAt(new ESString(str.substring(beg,end)),i);
}
return resultArray;
} else {
return ESNull.theNull;
}
}
}
class StringPrototypeSplit extends BuiltinFunctionObject {
StringPrototypeSplit(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
String str = thisObject.toString();
ESObject ap = this.evaluator.getArrayPrototype();
ArrayPrototype theArray = new ArrayPrototype(ap, this.evaluator);
if (arguments.length<=0) {
theArray.setSize(1);
theArray.setElementAt(thisObject, 0);
} else {
if (arguments[0] instanceof ESORORegExp) {
ESORORegExp pattern = (ESORORegExp) arguments[0];
int n = Util.SPLIT_ALL;
if (arguments.length>1) {
n = arguments[1].toUInt32();
}
Vector result = Util.split(matcher, pattern.getPattern(), str, n);
int l = result.size();
theArray.setSize(l);
for (int i=0; i<l; i++) {
theArray.setElementAt(
new ESString((String)result.elementAt(i)), i);
}
} else { // ! instanceof ESORORegExp
String sep = arguments[0].toString();
if (sep.length()==0) {
int l = str.length();
theArray.setSize(l);
for (int i=0; i<l; i++) {
theArray.setElementAt(
new ESString(str.substring(i,i+1)), i);
}
} else {
int i = 0;
int start = 0;
while (start<str.length()) {
int pos = str.indexOf(sep, start);
if (pos<0) pos = str.length();
// System.out.println("start: " + start + ", pos: " + pos);
theArray.setSize(i+1);
theArray.setElementAt(
new ESString(str.substring(start, pos)),i);
start = pos + sep.length();
i++;
}
}
} // instanceof ESORORegExp
}
return theArray;
}
}
ESObject stringPrototype = evaluator.getStringPrototype();
stringPrototype.putHiddenProperty("search",
new StringPrototypeSearch("search", evaluator, fp));
stringPrototype.putHiddenProperty("replace",
new StringPrototypeReplace("replace", evaluator, fp));
stringPrototype.putHiddenProperty("match",
new StringPrototypeMatch("match", evaluator, fp));
stringPrototype.putHiddenProperty("split",
new StringPrototypeSplit("split", evaluator, fp));
}
}

View file

@ -0,0 +1,97 @@
// OptionalRegExp.java
// FESI Copyright (c) Jean-Marc Lugrin, 1999
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package FESI.Extensions;
import FESI.Parser.*;
import FESI.AST.*;
import FESI.Interpreter.*;
import FESI.Exceptions.*;
import FESI.Data.*;
/**
* Create the regular expression object from either the
* OROINC library or the GNU regexp libray depending which
* one (if any) is available).
*/
public class OptionalRegExp extends Extension {
private Evaluator evaluator = null;
/**
* A dummy object used if no regular expression tool can be found
*/
class GlobalObjectRegExp extends BuiltinFunctionObject {
GlobalObjectRegExp(String name, Evaluator evaluator, FunctionPrototype fp) {
super(fp, evaluator, name, 1);
}
public ESValue callFunction(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
return doConstruct(thisObject, arguments);
}
public ESObject doConstruct(ESObject thisObject,
ESValue[] arguments)
throws EcmaScriptException {
throw new EcmaScriptException("RegExp package not loaded, probably not on CLASSPATH");
}
}
public OptionalRegExp () {
super();
}
/**
* Load the library at extension initialization time
*/
public void initializeExtension(Evaluator evaluator) throws EcmaScriptException {
Object regExp = null; // none fond
this.evaluator = evaluator;
// First attempt using ORO (as it is of higher quality)
regExp = evaluator.addExtension("FESI.Extensions.ORORegExp");
if (regExp == null) {
// Then attempt using GNU (as it is LGPL)
regExp = evaluator.addExtension("FESI.Extensions.GNURegExp");
}
// If neither is present, make a dummy object which will generate an error
// if the user attempt to use regluar expression
if (regExp == null) {
GlobalObject go = evaluator.getGlobalObject();
FunctionPrototype fp = (FunctionPrototype) evaluator.getFunctionPrototype();
ESObject globalObjectRegExp =
new GlobalObjectRegExp("RegExp", evaluator, fp);
globalObjectRegExp.putHiddenProperty("length",new ESNumber(1));
go.putHiddenProperty("RegExp", globalObjectRegExp);
}
}
}