1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.millscript.syntax;
22
23 import org.millscript.millscript.expr.Expr;
24
25 /**
26 * This class is a convenience for implementing procedure synyax elements. e.g.
27 * functions, fun's, methods and memoes.
28 */
29 public abstract class ProcedureSyntax extends PrefixSyntax {
30
31 /**
32 * The closing character sequence for this proceedure.
33 */
34 private final String closingKeyword;
35
36 /**
37 * Indicates if this proceedure requires a name. e.g. for use in lambda
38 * functions which have no name.
39 */
40 private boolean proceedureNeedsName;
41
42 /**
43 * Creates a proceedure syntax parser instance, with the specified closing
44 * character sequence.
45 *
46 * @param closing the closing character seqeunce for this procedure
47 * syntax parser instance
48 * @param nameNeeded a boolean indicating if this function must have a
49 * name or not.
50 */
51 protected ProcedureSyntax( final String closing, final boolean nameNeeded ) {
52 this.closingKeyword = closing;
53 this.proceedureNeedsName = nameNeeded;
54 }
55
56 /**
57 * @see org.millscript.millscript.syntax.PrefixSyntaxInterface#prefix(java.lang.String, org.millscript.millscript.syntax.Parser)
58 */
59 @Override
60 public Expr prefix( final String sym, final Parser parser ) {
61
62 Expr h = parser.readExprTo( "=>" );
63
64 FunctionHeader header = new FunctionHeader();
65
66 header.flatten( h, this.proceedureNeedsName );
67
68
69 Expr body = parser.readStmntsTo( this.closingKeyword );
70
71 return procedure( header, body, parser );
72 }
73
74 /**
75 * Returns the expression for this type of proceedure syntax.
76 *
77 * @param header the procedure header
78 * @param body the procedure body
79 * @param parser the current parser
80 * @return an Expr for this procedure
81 */
82 public abstract Expr procedure( final FunctionHeader header, final Expr body, final Parser parser );
83
84 }