Package org.jrubyparser.ast

Examples of org.jrubyparser.ast.Node


    private Module loadFunctionsFromFile(File file, Module parent) {
        try {
            Properties docNodes = collectDocNodes(file);
            Module module = new Module(getModuleName(file), true, parent);
            Parser parser = new Parser();
            Node node = parser.parse(file.getName(), new FileReader(file), new ParserConfiguration());
            List<Node> defnNodes = findNodes(node, new INodeFilter() {

                public void visitStart(Node node) {
                }
View Full Code Here


        if (argsNode.getBlock() != null)
            return;
        ListNode pre = argsNode.getPre();
        if (pre != null) {
            for (int i = 0; i < pre.size(); i++) {
                Node node = pre.get(i);
                if (node instanceof INameNode)
                    args.add(new Argument(((INameNode) node).getName()));
                else
                    return;
            }
        }
        ListNode optional = argsNode.getOptional();
        if (optional != null) {
            for (int i = 0; i < optional.size(); i++) {
                Node node = optional.get(i);
                if (!(node instanceof LocalAsgnNode))
                    return;
                LocalAsgnNode lan = (LocalAsgnNode) node;
                Node valueNode = lan.getValueNode();
                if ((valueNode instanceof StrNode))
                    args.add(new Argument(lan.getName(), argEncode(((StrNode) valueNode).getValue().toString()), Type.STRING));
                else if (valueNode instanceof RegexpNode)
                    args.add(new Argument(lan.getName(), ((RegexpNode) valueNode).getValue().toString(), Type.REGEX));
                else if (valueNode instanceof BignumNode)
                    args.add(new Argument(lan.getName(), "" + ((BignumNode) valueNode).getValue().toString(), Type.NUMBER));
                else if (valueNode instanceof FixnumNode)
                    args.add(new Argument(lan.getName(), ((FixnumNode) valueNode).getValue() + "", Type.NUMBER));
                else if (valueNode instanceof FloatNode)
                    args.add(new Argument(lan.getName(), ((FloatNode) valueNode).getValue() + "", Type.NUMBER));
                else if (valueNode instanceof TrueNode)
                    args.add(new Argument(lan.getName(), ((TrueNode) valueNode).getName(), Type.BOOLEAN));
                else if (valueNode instanceof FalseNode)
                    args.add(new Argument(lan.getName(), ((FalseNode) valueNode).getName(), Type.BOOLEAN));
                else if (valueNode instanceof ArrayNode && ((ArrayNode) valueNode).size() > 0) {
                    ArrayNode arrayNode = (ArrayNode) valueNode;
                    List<String> argValue = getListValue(arrayNode);
                    if (argValue != null) {
                        Node node2 = arrayNode.get(0);
                        Type type = Type.NONE;
                        if (node2 instanceof StrNode)
                            type = Type.STRING;
                        else if (node2 instanceof RegexpNode)
                            type = Type.REGEX;
View Full Code Here

    private List<String> getListValue(ArrayNode arrayNode) {
        boolean isString = false;
        boolean isRegex = false;
        boolean isNumber = false;
        Node node = arrayNode.get(0);
        if (node instanceof StrNode)
            isString = true;
        else if (node instanceof RegexpNode)
            isRegex = true;
        else if (node instanceof BignumNode || node instanceof FixnumNode || node instanceof FloatNode)
            isNumber = true;
        else
            return null;

        List<String> l = new ArrayList<String>();
        for (int i = 0; i < arrayNode.size(); i++) {
            Node anode = arrayNode.get(i);
            if (isString && anode instanceof StrNode) {
                l.add(argEncode(((StrNode) anode).getValue().toString()));
            } else if (isRegex && anode instanceof RegexpNode) {
                l.add("/" + ((RegexpNode) anode).getValue().toString() + "/");
            } else if (isNumber && anode instanceof BignumNode) {
View Full Code Here

            return null;
        return validWithCallNode(callNodes[0]);
    }

    private String validWithCallNode(FCallNode node) {
        Node argsNode = node.getArgsNode();
        if (!(argsNode instanceof ArrayNode))
            return null;
        ArrayNode aNode = (ArrayNode) argsNode;
        if (aNode.size() != 1)
            return null;
        Node node2 = aNode.get(0);
        if (!(node2 instanceof StrNode))
            return null;
        return ((StrNode) node2).getValue();
    }
View Full Code Here

        RubyParserUtils.parseFile(providerFile).getBody(), new NodeType[] { NodeType.CALLNODE })) {
        CallNode call = (CallNode) node;
        if(!"provide".equals(call.getName()))
          continue;

        Node receiverNode = call.getReceiver();
        if(!(receiverNode instanceof CallNode))
          continue;

        CallNode receiver = (CallNode) receiverNode;
        if(!"type".equals(receiver.getName()))
          continue;
        Node recRecNode = receiver.getReceiver();
        if(!(recRecNode instanceof Colon2ConstNode))
          continue;
        Colon2ConstNode recRec = (Colon2ConstNode) recRecNode;
        if(!("Puppet".equals(((ConstNode) recRec.getLeftNode()).getName()) && "Type".equals(recRec.getName())))
          continue;
View Full Code Here

      // Find the newtype call
      nodes = RubyParserUtils.findNodes(puppetModule.getBody(), new NodeType[] { NodeType.CALLNODE });
      for(Node node : nodes) {
        CallNode call = (CallNode) node;
        if("newtype".equals(call.getName())) {
          Node receiver = call.getReceiver();
          if(receiver instanceof ConstNode && "Type".equals(((ConstNode) receiver).getName())) {
            newtypeNode = call;
            break;
          }
        }
      }
      if(newtypeNode == null) {
        // Try syntax found in iptables.rb. Not sure it's correct
        // but it seems to be parsed
        // OK by the puppet-tool
        nodes = RubyParserUtils.findNodes(puppetModule.getBody(), new NodeType[] { NodeType.FCALLNODE });
        for(Node node : nodes) {
          FCallNode call = (FCallNode) node;
          if("newtype".equals(call.getName())) {
            newtypeNode = call;
            break;
          }
        }
      }
    }
    else {
      // The call might be a CallNode at the top level
      nodes = RubyParserUtils.findNodes((root).getBody(), new NodeType[] { NodeType.CALLNODE });
      for(Node node : nodes) {
        CallNode call = (CallNode) node;
        if("newtype".equals(call.getName())) {
          Node receiver = call.getReceiver();
          if(receiver instanceof Colon2ConstNode) {
            Colon2ConstNode c2cNode = (Colon2ConstNode) receiver;
            if("Type".equals(c2cNode.getName()) && c2cNode.getLeftNode() instanceof ConstNode &&
                "Puppet".equals(((ConstNode) c2cNode.getLeftNode()).getName())) {
              newtypeNode = call;
              break;
            }
          }
        }
      }
    }

    if(newtypeNode == null)
      throw new IOException("Unable to find newtype call in " + typeFileStr);

    // Find the parameter that is passed in the call to newtype. It must
    // be one
    // single parameter in the form of a Symbol. This Symbol denotes the
    // name of
    // the new type.
    Node argsNode = ((IArgumentNode) newtypeNode).getArgs();
    nodes = RubyParserUtils.findNodes(argsNode, new NodeType[] { NodeType.SYMBOLNODE });
    if(nodes.size() != 1)
      throw new IOException("The newtype call does not take exactly one symbol parameter in " + typeFileStr);

    SymbolNode typeName = (SymbolNode) nodes.get(0);
    type.setName(typeName.getName());

    // Find the assignment of the @doc instance variable
    Node iterNode = newtypeNode.getIter();
    nodes = RubyParserUtils.findNodes(iterNode, new NodeType[] { NodeType.BLOCKNODE, NodeType.INSTASGNNODE });
    if(nodes.isEmpty())
      // No block when there's just one assignment
      nodes = RubyParserUtils.findNodes(iterNode, new NodeType[] { NodeType.INSTASGNNODE });

    for(Node node : nodes) {
      InstAsgnNode asgnNode = (InstAsgnNode) node;
      if(!"@doc".equals(asgnNode.getName()))
        continue;

      Node valueNode = asgnNode.getValue();
      if(valueNode instanceof StrNode)
        type.setDocumentation(((StrNode) valueNode).getValue());
      break;
    }
View Full Code Here

   * @return
   */
  public PPTypeInfo findMetaTypeInfo(Node root) {
    Map<String, PPTypeInfo.Entry> parameterMap = Maps.newHashMap();
    RubyModuleFinder moduleFinder = new RubyModuleFinder();
    Node module = moduleFinder.findModule(root, new String[] { "Puppet" });
    for(Node n : module.childNodes()) {
      if(n.getNodeType() == NodeType.NEWLINENODE)
        n = ((NewlineNode) n).getNextNode();
      if(n.getNodeType() == NodeType.CLASSNODE) {
        ClassNode classNode = (ClassNode) n;
        // could check if this is the class 'Type' but somewhat
View Full Code Here

   *         addition found.
   */
  public List<PPTypeInfo> findTypePropertyInfo(Node root) {
    List<PPTypeInfo> result = Lists.newArrayList();
    RubyModuleFinder moduleFinder = new RubyModuleFinder();
    Node module = moduleFinder.findModule(root, new String[] { "Puppet" });

    // Some property additions are in "Puppet" modules, some are not
    if(module == null)
      module = root.getNodeType() == NodeType.ROOTNODE
          ? ((RootNode) root).getBody()
          : root;
    OpCallVisitor opCallVisitor = new OpCallVisitor();
    for(Node n1 : module.childNodes()) {
      if(n1.getNodeType() == NodeType.NEWLINENODE)
        n1 = ((NewlineNode) n1).getNextNode();
      Iterable<Node> nodeIterable = null;
      if(n1.getNodeType() == NodeType.BLOCKNODE)
        nodeIterable = ((BlockNode) n1).childNodes();
View Full Code Here

  }

  PPTypeInfo.Entry getEntry(BlockAcceptingNode callNode) {
    String desc = "";
    boolean namevar = false;
    Node bodyNode = safeGetBodyNode(callNode);
    if(bodyNode != null)
      for(Node n : bodyNode.childNodes()) {
        if(n.getNodeType() == NodeType.NEWLINENODE)
          n = ((NewlineNode) n).getNextNode();
        if(n.getNodeType() == NodeType.FCALLNODE) {
          FCallNode cn = (FCallNode) n;
          if("desc".equals(cn.getName()))
View Full Code Here

        ? defaultValue
        : x;
  }

  private Node safeGetBodyNode(BlockAcceptingNode node) {
    Node n = node.getIter();
    if(n == null)
      return null;
    switch(n.getNodeType()) {
      case ITERNODE:
        return ((IterNode) n).getBody();
      case BLOCKPASSNODE:
        return ((BlockPassNode) n).getBody();
      default:
View Full Code Here

TOP

Related Classes of org.jrubyparser.ast.Node

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.