Search in sources :

Example 66 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project kalang by kasonyang.

the class AstBuilder method visitInterpolationExpr.

@Override
public Object visitInterpolationExpr(KalangParser.InterpolationExprContext ctx) {
    List<ParseTree> children = ctx.children;
    ExprNode[] exprs = new ExprNode[children.size()];
    Token[] exprTokens = new Token[children.size()];
    for (int i = 0; i < exprs.length; i++) {
        ParseTree c = children.get(i);
        if (c instanceof TerminalNode) {
            Token token = ((TerminalNode) c).getSymbol();
            int t = token.getType();
            String rawText = c.getText();
            String text;
            switch(t) {
                case KalangLexer.InterpolationPreffixString:
                    text = rawText.substring(1, rawText.length() - 2);
                    break;
                case KalangLexer.INTERPOLATION_STRING:
                    text = rawText;
                    break;
                case KalangLexer.RBRACE:
                case KalangLexer.INTERPOLATION_END:
                case KalangLexer.INTERPOLATION_INTERUPT:
                    // TODO optimize empty string
                    text = "";
                    break;
                default:
                    throw Exceptions.unexceptedValue(t);
            }
            exprs[i] = new ConstExpr(StringLiteralUtil.parse(text));
            exprTokens[i] = token;
        } else if (c instanceof ExpressionContext) {
            ExprNode expr = this.visitExpression((ExpressionContext) c);
            if (expr == null)
                return null;
            exprs[i] = expr;
            exprTokens[i] = ((ExpressionContext) c).getStart();
        } else {
            throw Exceptions.unexceptedValue(c);
        }
    }
    return this.concatExpressionsToStringExpr(exprs, exprTokens);
}
Also used : ExprNode(kalang.ast.ExprNode) ConstExpr(kalang.ast.ConstExpr) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) Token(org.antlr.v4.runtime.Token) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 67 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.

the class ConfigTreeNodeUtilities method addChild.

/**
 * Adds a child to a parse tree
 * @param aParent parent node to add the child to
 * @param aChild child to add as parent's sibling
 * @param aIndex index, where child is added in the child list, special case: -1: added as last
 */
public static void addChild(final ParseTree aParent, final ParseTree aChild, final int aIndex) {
    if (aParent == null) {
        ErrorReporter.INTERNAL_ERROR("ConfigTreeNodeUtilities.addChild(): aParent == null");
        return;
    }
    if (aChild == null) {
        ErrorReporter.INTERNAL_ERROR("ConfigTreeNodeUtilities.addChild(): aChild == null");
        return;
    }
    if (aParent == aChild) {
        ErrorReporter.INTERNAL_ERROR("ConfigTreeNodeUtilities.addChild(): aParent == aChild");
        return;
    }
    if (aParent instanceof ParserRuleContext) {
        final ParserRuleContext rule = (ParserRuleContext) aParent;
        if (rule.children == null) {
            rule.children = new ArrayList<ParseTree>();
        }
        if (aIndex >= 0) {
            rule.children.set(aIndex, aChild);
        } else {
            rule.children.add(aChild);
        }
        setParent(aChild, rule);
    } else {
        ErrorReporter.INTERNAL_ERROR("ConfigTreeNodeUtilities.addChild(): only ParserRuleContext can have children");
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) AddedParseTree(org.eclipse.titan.common.parsers.AddedParseTree) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 68 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.

the class ConfigTreeNodeUtilities method removeChild.

/**
 * Removes child from parent's list.
 * Parent is get from child data.
 * @param aChild child element to remove
 */
public static void removeChild(final ParseTree aChild) {
    if (aChild == null) {
        ErrorReporter.INTERNAL_ERROR("ConfigTreeNodeUtilities.removeChild( ParseTree ): aChild == null");
        return;
    }
    final ParseTree parent = aChild.getParent();
    removeChild(parent, aChild, false);
}
Also used : AddedParseTree(org.eclipse.titan.common.parsers.AddedParseTree) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 69 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.

the class ComponentSectionDropTargetListener method drop.

@Override
public void drop(final DropTargetEvent event) {
    if (ComponentItemTransfer.getInstance().isSupportedType(event.currentDataType)) {
        if (event.item != null && viewer.getInput() != null) {
            ComponentSectionHandler componentSectionHandler = (ComponentSectionHandler) viewer.getInput();
            Component element = (Component) event.item.getData();
            Component[] items = (Component[]) event.data;
            int baseindex = componentSectionHandler.getComponents().indexOf(element);
            final ParseTree parent = componentSectionHandler.getLastSectionRoot();
            ConfigTreeNodeUtilities.removeChild(parent, element.getRoot());
            ConfigTreeNodeUtilities.addChild(parent, element.getRoot(), baseindex);
            if (items.length > 0) {
                for (int i = 0; i < items.length - 1; i++) {
                    componentSectionHandler.getComponents().add(++baseindex, items[i]);
                }
                componentSectionHandler.getComponents().add(++baseindex, items[items.length - 1]);
            }
            viewer.refresh(true);
            editor.setDirty();
        }
    }
}
Also used : ComponentSectionHandler(org.eclipse.titan.common.parsers.cfg.indices.ComponentSectionHandler) Component(org.eclipse.titan.common.parsers.cfg.indices.ComponentSectionHandler.Component) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 70 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.

the class GroupsSubPage method createGroupItemsSection.

private void createGroupItemsSection(final Composite parent, final ScrolledForm form, final FormToolkit toolkit) {
    Section section = toolkit.createSection(parent, Section.DESCRIPTION | ExpandableComposite.TITLE_BAR);
    section.setActiveToggleColor(toolkit.getHyperlinkGroup().getActiveForeground());
    section.setToggleColor(toolkit.getColors().getColor(IFormColors.SEPARATOR));
    Composite client = toolkit.createComposite(section, SWT.WRAP);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    client.setLayout(layout);
    itemsTable = toolkit.createTable(client, SWT.NULL);
    itemsTable.setEnabled(groupSectionHandler != null && selectedGroup != null);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.heightHint = 200;
    gd.widthHint = 100;
    itemsTable.setLayoutData(gd);
    toolkit.paintBordersFor(client);
    itemsTable.setLinesVisible(true);
    itemsTable.setHeaderVisible(true);
    Composite buttons = toolkit.createComposite(client);
    buttons.setLayout(new GridLayout());
    buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
    addItem = toolkit.createButton(buttons, "Add item", SWT.PUSH);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
    addItem.setLayoutData(gd);
    addItem.setEnabled(groupSectionHandler != null && selectedGroup != null);
    addItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(final SelectionEvent e) {
            if (groupSectionHandler == null) {
                return;
            }
            if (selectedGroup == null) {
                return;
            }
            GroupItem newItem;
            ParseTree hidden = new AddedParseTree(", ");
            ConfigTreeNodeUtilities.addChild(selectedGroup.getRoot(), hidden);
            ParseTree node = new AddedParseTree("item");
            ConfigTreeNodeUtilities.addChild(selectedGroup.getRoot(), node);
            newItem = new GroupSectionHandler.GroupItem(node);
            selectedGroup.getGroupItems().add(newItem);
            internalRefresh();
            itemsTable.select(selectedGroup.getGroupItems().size() - 1);
            itemsTable.showSelection();
            editor.setDirty();
        }
    });
    removeItem = toolkit.createButton(buttons, "Remove item", SWT.PUSH);
    gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
    removeItem.setLayoutData(gd);
    removeItem.setEnabled(groupSectionHandler != null && selectedGroup != null);
    removeItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(final SelectionEvent e) {
            if (itemsTableViewer == null || groupSectionHandler == null) {
                return;
            }
            removeSelectedItems();
            internalRefresh();
            editor.setDirty();
        }
    });
    totalGroupItemsLabel = toolkit.createLabel(buttons, "");
    if (selectedGroup == null) {
        totalGroupItemsLabel.setText("Total: ");
    } else {
        totalGroupItemsLabel.setText("Total: " + selectedGroup.getGroupItems().size());
    }
    gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
    totalGroupItemsLabel.setLayoutData(gd);
    section.setText("Group items");
    section.setDescription("Specify items of the selected group.");
    section.setClient(client);
    section.setExpanded(true);
    section.addExpansionListener(new ExpansionAdapter() {

        @Override
        public void expansionStateChanged(final ExpansionEvent e) {
            form.reflow(false);
        }
    });
    gd = new GridData(GridData.FILL_BOTH);
    section.setLayoutData(gd);
    TableColumn column = new TableColumn(itemsTable, SWT.LEFT, 0);
    column.setText("Group item");
    column.setWidth(150);
    column.setMoveable(false);
    itemsTableViewer = new TableViewer(itemsTable);
    itemsTableViewer.setContentProvider(new GroupItemDataContentProvider());
    itemsTableViewer.setLabelProvider(new GroupItemDataLabelProvider());
    itemsTableViewer.setInput(null);
    itemsTableViewer.setColumnProperties(new String[] { "groupItem" });
    itemsTableViewer.setCellEditors(new TextCellEditor[] { new TextCellEditor(itemsTable) });
    itemsTableViewer.setCellModifier(new ICellModifier() {

        @Override
        public boolean canModify(final Object element, final String property) {
            return true;
        }

        @Override
        public Object getValue(final Object element, final String property) {
            GroupItemDataLabelProvider labelProvider = (GroupItemDataLabelProvider) itemsTableViewer.getLabelProvider();
            return labelProvider.getColumnText(element, 0);
        }

        @Override
        public void modify(final Object element, final String property, final Object value) {
            if (element != null && element instanceof TableItem && value instanceof String) {
                GroupItem item = (GroupItem) ((TableItem) element).getData();
                ConfigTreeNodeUtilities.setText(item.getItem(), (String) value);
                itemsTableViewer.refresh(item);
                editor.setDirty();
            }
        }
    });
}
Also used : Composite(org.eclipse.swt.widgets.Composite) ExpandableComposite(org.eclipse.ui.forms.widgets.ExpandableComposite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) TableItem(org.eclipse.swt.widgets.TableItem) ExpansionAdapter(org.eclipse.ui.forms.events.ExpansionAdapter) AddedParseTree(org.eclipse.titan.common.parsers.AddedParseTree) Section(org.eclipse.ui.forms.widgets.Section) TableColumn(org.eclipse.swt.widgets.TableColumn) GridLayout(org.eclipse.swt.layout.GridLayout) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ICellModifier(org.eclipse.jface.viewers.ICellModifier) GroupItem(org.eclipse.titan.common.parsers.cfg.indices.GroupSectionHandler.GroupItem) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) ExpansionEvent(org.eclipse.ui.forms.events.ExpansionEvent) TableViewer(org.eclipse.jface.viewers.TableViewer) AddedParseTree(org.eclipse.titan.common.parsers.AddedParseTree) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)311 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)104 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)60 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)49 AddedParseTree (org.eclipse.titan.common.parsers.AddedParseTree)46 CharStream (org.antlr.v4.runtime.CharStream)43 Test (org.junit.Test)43 CommonToken (org.antlr.v4.runtime.CommonToken)35 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)35 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 File (java.io.File)26 ArrayList (java.util.ArrayList)22 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)22 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 Grammar (org.antlr.v4.tool.Grammar)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 IOException (java.io.IOException)19 LexerGrammar (org.antlr.v4.tool.LexerGrammar)16