(CommonTree t, Map<String, PigMacro> seen)
| 442 | * 2. MACRO_DEF (PARAMS, RETURN_VAL, MACRO_BODY) |
| 443 | */ |
| 444 | private PigMacro makeMacroDef(CommonTree t, Map<String, PigMacro> seen) |
| 445 | throws ParserException { |
| 446 | String mn = t.getChild(0).getText(); |
| 447 | |
| 448 | if (!macroSeen.add(mn)) { |
| 449 | String msg = getErrorMessage(null, t, null, |
| 450 | "Duplicated macro name '" + mn + "'"); |
| 451 | throw new ParserException(msg); |
| 452 | } |
| 453 | |
| 454 | if (seen != null) { |
| 455 | for (String s : seen.keySet()) { |
| 456 | macroSeen.add(s); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | String fname = ((PigParserNode)t).getFileName(); |
| 461 | |
| 462 | Tree defNode = t.getChild(1); |
| 463 | |
| 464 | // get parameter markers |
| 465 | ArrayList<String> params = new ArrayList<String>(); |
| 466 | Tree paramNode = defNode.getChild(0); |
| 467 | int n = paramNode.getChildCount(); |
| 468 | for (int i = 0; i < n; i++) { |
| 469 | params.add(paramNode.getChild(i).getText()); |
| 470 | } |
| 471 | |
| 472 | // get return alias markers |
| 473 | ArrayList<String> returns = new ArrayList<String>(); |
| 474 | Tree retNode = defNode.getChild(1); |
| 475 | int m = retNode.getChildCount(); |
| 476 | for (int i = 0; i < m; i++) { |
| 477 | returns.add(retNode.getChild(i).getText()); |
| 478 | } |
| 479 | |
| 480 | // get macro body |
| 481 | Tree bodyNode = defNode.getChild(2); |
| 482 | String body = bodyNode.getChild(0).getText(); |
| 483 | |
| 484 | body = body.substring(1, body.length() - 1); |
| 485 | |
| 486 | // sometimes the script has no filename, like when a string is passed to PigServer for |
| 487 | // example. See PIG-2866. |
| 488 | if (!fname.isEmpty()) { |
| 489 | FetchFileRet localFileRet = getMacroFile(fname); |
| 490 | fname = localFileRet.file.getAbsolutePath(); |
| 491 | } |
| 492 | |
| 493 | PigMacro pm = new PigMacro(mn, fname, params, returns, body, seen); |
| 494 | |
| 495 | try { |
| 496 | pm.validate(); |
| 497 | } catch (IOException e) { |
| 498 | String msg = getErrorMessage(null, t, |
| 499 | "Invalid macro definition: ", e.getMessage()); |
| 500 | throw new ParserException(msg); |
| 501 | } |
no test coverage detected