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);
}
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");
}
}
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);
}
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();
}
}
}
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();
}
}
});
}
Aggregations