山东大学软件工程应用与实践——PIG代码分析(一)

2021SC@SDUSC

总览

解析器parser检查脚本的语法、类型和其他一些杂项,之后解析器的输出表示pigLatin语句以及逻辑运算符。本次源码分析主要分析的是parser包的两个工具类,QueryParserStreamUtil类和StreamingCommandUtils类的部分方法

QueryParserStreamUtil类

LA()方法

用于协助 QueryParserFileStream类实现使 ANTLR 不区分大小写同时保留大小写。

 public static int LA(int i, int n, int p, char[] data) {
    
    
        if( i == 0 ) {
    
    
            return 0; // undefined
        }
        
        if ( i < 0 ) {
    
    
            i++; // e.g., translate LA(-1) to use offset 0
        }

        if( p + i - 1 >= n ) {
    
    
            return CharStream.EOF;
        }
        
        return Character.toUpperCase( data[ p + i - 1 ] );
    }

StreamingCommandUtils类

流命令相关的方法

isQuotedString()方法

检查文件是否在要跳过的列表路径中,如果在返回true,反之返回false。

private boolean inSkipPaths(String file) {
    
    
         for (String skipPath : pigContext.getPathsToSkip()) {
    
    
             if (file.startsWith(skipPath)) {
    
    
                 return true;
             }
         }
        return false;
     }

checkAndShip() 方法

如果命令不是python或者Perl命令,则调用该方法将流添加到要跳过的列表路径中。

private void checkAndShip(StreamingCommand command, String arg) 
    throws ParserException {
    
    
        // Don't auto-ship if it is an absolute path...
        if (arg.startsWith("/")) {
    
    
            return;
        }

        // $ which arg
        String argPath = whichCache.getUnchecked(arg);
        if (argPath.length() > 0 && !inSkipPaths(argPath)) {
    
    
            try {
    
    
                command.addPathToShip(argPath);
            } catch(IOException e) {
    
    
               ParserException pe = new ParserException(e.getMessage());
               pe.initCause(e);
               throw pe;
           }
        }
         
    }

checkAutoShipSpecs() 方法

首先检测命令是否为python或者Perl,如果是的话检测字符串,如果不是以-开头则将流添加到要跳过列表路径中。

void checkAutoShipSpecs(StreamingCommand command, String[] argv) 
    throws ParserException {
    
    
        // Candidate for auto-ship
        String arg0 = argv[0];
        
        // Check if command is perl or python ... if so use the first non-option
        // and non-quoted string as the candidate
       if (arg0.equalsIgnoreCase(PERL) || arg0.equalsIgnoreCase(PYTHON)) {
    
    
           for (int i=1; i < argv.length; ++i) {
    
    
               if (!argv[i].startsWith("-") && !isQuotedString(argv[i])) {
    
    
                   checkAndShip(command, argv[i]);
                   break;
               }
           }
       } else {
    
    
           // Ship the first argument if it can be ...
           checkAndShip(command, arg0);
       }
    }

总结

本周对PIG解析器的部分工具类进行分析,了解到解析器部分工具类中方法的调用流程以及用法,方便了对之后代码分析的过程。

猜你喜欢

转载自blog.csdn.net/qq_45822693/article/details/120682030