Search in sources :

Example 1 with PropertyExp

use of priv.bajdcc.LALR1.syntax.exp.PropertyExp in project jMiniLang by bajdcc.

the class Syntax method matchStorage.

/**
 * 匹配存储序号
 *
 * @param exp
 *            表达式
 * @param storage
 *            存储序号
 * @return 属性对象
 * @throws SyntaxException 词法错误
 */
private PropertyExp matchStorage(ISyntaxComponent exp, Object storage) throws SyntaxException {
    PropertyExp property;
    if (token.kToken == TokenType.STORAGE) {
        property = new PropertyExp((int) storage, null);
        next();
    } else {
        property = new PropertyExp(-1, null);
    }
    property.expression = exp;
    return property;
}
Also used : PropertyExp(priv.bajdcc.LALR1.syntax.exp.PropertyExp)

Example 2 with PropertyExp

use of priv.bajdcc.LALR1.syntax.exp.PropertyExp in project jMiniLang by bajdcc.

the class Syntax method doAnalysis.

/**
 * 分析表达式
 *
 * @param type
 *            结束类型
 * @param obj
 *            结束数据
 * @return 表达式树根结点
 * @throws SyntaxException 词法错误
 */
private ISyntaxComponent doAnalysis(TokenType type, Object obj) throws SyntaxException {
    /* 新建序列用于存放结果 */
    SequenceExp sequence = new SequenceExp();
    /* 可能会使用的分支 */
    BranchExp branch = null;
    /* 添加子结点接口 */
    IExpCollction collection = sequence;
    /* 表达式通用接口 */
    ISyntaxComponent result = sequence;
    for (; ; ) {
        if ((token.kToken == type && (token.object == null || token.object.equals(obj)))) {
            // 结束字符
            if (syntaxLexer.index() == 0) {
                // 表达式为空
                err(SyntaxError.NULL);
            } else if (collection.isEmpty()) {
                // 部件为空
                err(SyntaxError.INCOMPLETE);
            } else {
                next();
                // 正常终止
                break;
            }
        } else if (token.kToken == TokenType.EOF) {
            err(SyntaxError.INCOMPLETE);
        }
        // 当前待赋值的表达式
        ISyntaxComponent exp = null;
        switch(token.kToken) {
            case OPERATOR:
                OperatorType op = (OperatorType) token.object;
                next();
                switch(op) {
                    case ALTERNATIVE:
                        if (// 在此之前没有存储表达式 (|...)
                        collection.isEmpty()) {
                            err(SyntaxError.INCOMPLETE);
                        } else {
                            if (branch == null) {
                                // 分支为空,则建立分支
                                branch = new BranchExp();
                                // 用新建的分支包含并替代当前序列
                                branch.add(sequence);
                                result = branch;
                            }
                            // 新建一个序列
                            sequence = new SequenceExp();
                            branch.add(sequence);
                            continue;
                        }
                        break;
                    case // '('
                    LPARAN:
                        // 递归分析
                        exp = doAnalysis(TokenType.OPERATOR, OperatorType.RPARAN);
                        break;
                    case // '['
                    LSQUARE:
                        // 递归分析
                        exp = doAnalysis(TokenType.OPERATOR, OperatorType.RSQUARE);
                        // 包装
                        OptionExp option = new OptionExp();
                        option.expression = exp;
                        exp = option;
                        break;
                    default:
                        err(SyntaxError.SYNTAX);
                        break;
                }
                break;
            case EOF:
                return result;
            case TERMINAL:
                exp = matchTerminal();
                next();
                PropertyExp property1 = matchStorage(exp, token.object);
                exp = property1;
                if (token.kToken == TokenType.ACTION) {
                    property1.actionHandler = matchAction();
                    next();
                }
                if (token.kToken == TokenType.HANDLER) {
                    property1.errorHandler = matchHandler();
                    next();
                }
                break;
            case NONTERMINAL:
                exp = matchNonTerminal();
                next();
                PropertyExp property2 = matchStorage(exp, token.object);
                exp = property2;
                if (token.kToken == TokenType.ACTION) {
                    property2.actionHandler = matchAction();
                    next();
                }
                if (token.kToken == TokenType.HANDLER) {
                    property2.errorHandler = matchHandler();
                    next();
                }
                break;
            default:
                err(SyntaxError.SYNTAX);
                break;
        }
        if (exp != null) {
            sequence.add(exp);
        }
    }
    return result;
}
Also used : IExpCollction(priv.bajdcc.LALR1.syntax.exp.IExpCollction) BranchExp(priv.bajdcc.LALR1.syntax.exp.BranchExp) SequenceExp(priv.bajdcc.LALR1.syntax.exp.SequenceExp) OptionExp(priv.bajdcc.LALR1.syntax.exp.OptionExp) PropertyExp(priv.bajdcc.LALR1.syntax.exp.PropertyExp) OperatorType(priv.bajdcc.LALR1.syntax.token.OperatorType)

Aggregations

PropertyExp (priv.bajdcc.LALR1.syntax.exp.PropertyExp)2 BranchExp (priv.bajdcc.LALR1.syntax.exp.BranchExp)1 IExpCollction (priv.bajdcc.LALR1.syntax.exp.IExpCollction)1 OptionExp (priv.bajdcc.LALR1.syntax.exp.OptionExp)1 SequenceExp (priv.bajdcc.LALR1.syntax.exp.SequenceExp)1 OperatorType (priv.bajdcc.LALR1.syntax.token.OperatorType)1