|
| 1 | +# Java FFI DSL Syntax |
| 2 | +The Java FFI DSL is used to describe the classes and interfaces of a |
| 3 | +java package, and their constructors, methods, variables and constants. |
| 4 | + |
| 5 | +All java class definitions should have a unique representation in the |
| 6 | +DSL (indeed in many cases it will be very similar) |
| 7 | + |
| 8 | +## DSL syntax |
| 9 | +### Package definition |
| 10 | +A package consists of a declaration of the Name, a ModuleName which is |
| 11 | +used for the LiveCode Builder output module, and a collection of class |
| 12 | +and interface definitions, and use clauses. |
| 13 | + |
| 14 | + Package |
| 15 | + : "foreign" "package" <Name: QualifiedName> "named" <ModuleName: STRING> SEPARATOR |
| 16 | + { Definition SEPARATOR } |
| 17 | + "end" "package" |
| 18 | + |
| 19 | +A definition is either a class or an interface definition, or a use clause. |
| 20 | + |
| 21 | + Definition |
| 22 | + : Use |
| 23 | + | Interface |
| 24 | + | Class |
| 25 | + |
| 26 | +### Use clauses |
| 27 | +A use clause specifies which other java classes and interfaces are used |
| 28 | +within this package. |
| 29 | + |
| 30 | + Use |
| 31 | + : "use" <PackageName: QualifiedName> "." <ClassName: STRING> |
| 32 | + |
| 33 | +### Class and interface definitions |
| 34 | +Class and interface definitions contain lists of the constants, |
| 35 | +variables, methods and constructors that are members of the class or |
| 36 | +interface. They also specify how the class or interface fits in to the |
| 37 | +java Object hierarchy. |
| 38 | + |
| 39 | + Interface |
| 40 | + : "interface" <Interface: ClassType> ["inherits" <Parents: InterfaceList>] SEPARATOR |
| 41 | + { InterfaceDef SEPARATOR } |
| 42 | + "end" "interface" |
| 43 | + |
| 44 | + Class |
| 45 | + : [ <Modifiers: ClassModifiers> ] "class" <Class: ClassType> ["inherits" <Parent: ClassType>] ["implements" <List: InterfaceList>] SEPARATOR |
| 46 | + { ClassDef SEPARATOR } |
| 47 | + "end" "class" |
| 48 | + |
| 49 | +Interfaces can only define methods and constants. |
| 50 | + |
| 51 | + InterfaceDef |
| 52 | + : [ <Modifiers: InterfaceMethodModifier> ] "method" <Name: STRING> <ParamList: Parameters> ["named" <Alias: STRING>] ["throws" <List: ClassList>] "returns" <ReturnType: Type> |
| 53 | + | "constant" <Name: STRING> "as" <VarType: Type> ["is" <Exp: Expression>] |
| 54 | + |
| 55 | +Classes can also define constructors and variables. |
| 56 | + |
| 57 | + ClassDef |
| 58 | + : [ <Modifiers: MethodModifiers> ] "method" <Name: STRING> <ParamList: Parameters> ["named" <Alias: STRING>] ["throws" <List: ClassList>] "returns" <ReturnType: Type> |
| 59 | + | [ <Modifiers: ConstructiorModifiers> ] "constructor" <Name: STRING> ["named" <Alias: STRING>] |
| 60 | + | [ <Modifiers: VariableModifiers> ] "variable" <Name: STRING> "as" <VarType: Type> |
| 61 | + | "constant" <Name: STRING> "as" <VarType: Type> ["is" <Exp: Expression>] |
| 62 | + |
| 63 | +### Implements / inherits |
| 64 | +Classes and interfaces can only implement/inherit other classes or |
| 65 | +interfaces of the appropriate type. |
| 66 | + |
| 67 | +The only difference between ClassList and InterfaceList is that it is a |
| 68 | +parse error if the items in InterfaceList are not defined as interfaces. |
| 69 | + |
| 70 | + InterfaceList |
| 71 | + : <Head: ClassTypeInstance> [ "," <Tail: InterfaceList> ] |
| 72 | + |
| 73 | + ClassList |
| 74 | + : <Head: ClassTypeInstance> [ "," <Tail: ClassList> ] |
| 75 | + |
| 76 | +### Modifiers |
| 77 | +Modifiers give extra information about how or in what context classes or |
| 78 | +their members can be used. For example, if the "class" instance modifier |
| 79 | +is applied to a method of a class, an instance of the class is not |
| 80 | +required in order to call that method (i.e. `static`, in java parlance). |
| 81 | + |
| 82 | +The majority of the modifier information is not currently used, but |
| 83 | +should be provided anyway so that better glue code can be generated in |
| 84 | +the future. |
| 85 | + |
| 86 | + ClassModifiers |
| 87 | + : [ <Access: AccessModifier> ] [ <StrictFP: StrictFPModifier> ] [ <Inheritance: InheritanceModifier> ] [ <Modify: ModifyModifier> ] [ <Instance: InstanceModifier> ] |
| 88 | + |
| 89 | + MethodModifiers |
| 90 | + : [ <Access: AccessModifier> ] [ <Sync: SyncModifier> ] [ <Native: NativeModifier> ] [ <StrictFP: StrictFPModifier> ] [ <Inheritance: InheritanceModifier> ] [ <Instance: InstanceModifier> ] |
| 91 | + |
| 92 | + ConstructorModifiers |
| 93 | + : [ <Access: AccessModifier> ] |
| 94 | + |
| 95 | + VariableModifiers |
| 96 | + : [ <Access: AccessModifier> ] [ <Transient: TransientModifier> ] [ <Modify: ModifyModifier> ] [ <Instance: InstanceModifier> ] |
| 97 | + |
| 98 | + InterfaceMethodModifier |
| 99 | + : "default" |
| 100 | + | "class" |
| 101 | + |
| 102 | + AccessModifier |
| 103 | + : "public" |
| 104 | + | "protected" |
| 105 | + |
| 106 | + SyncModifier |
| 107 | + : "synchronized" |
| 108 | + |
| 109 | + NativeModifier |
| 110 | + : "native" |
| 111 | + |
| 112 | + StrictFPModifier |
| 113 | + : "strictfp" |
| 114 | + |
| 115 | + InheritanceModifier |
| 116 | + : "abstract" |
| 117 | + : "final" |
| 118 | + |
| 119 | + InstanceModifier |
| 120 | + : "class" |
| 121 | + |
| 122 | + ModifyModifier |
| 123 | + : "final" |
| 124 | + | "volatile" |
| 125 | + |
| 126 | + TransientModifier |
| 127 | + : "transient" |
| 128 | + |
| 129 | + |
| 130 | +### Parameters |
| 131 | +Note: all java method parameters are `in` parameters from the LCB point |
| 132 | +of view. |
| 133 | + |
| 134 | + Parameters |
| 135 | + : "(" [ <Head: Parameter> [ "," <Tail: Parameters> ] ")" |
| 136 | + | "..." |
| 137 | + |
| 138 | + Parameter |
| 139 | + : <Name: STRING> "as" <ParamType: Type> |
| 140 | + |
| 141 | +### Types |
| 142 | +Types in Java are either primitive, derived from the Object class, or |
| 143 | +arrays of either of those. |
| 144 | + |
| 145 | + Type |
| 146 | + : "byte" |
| 147 | + | "short" |
| 148 | + | "int" |
| 149 | + | "long" |
| 150 | + | "float" |
| 151 | + | "double" |
| 152 | + | "boolean" |
| 153 | + | "char" |
| 154 | + | "String" |
| 155 | + | Type "[" "]" |
| 156 | + | ClassType |
| 157 | + |
| 158 | + ClassType |
| 159 | + : <Name: STRING> ["<" <Params: ClassTypeParamList> ">" ] |
| 160 | + |
| 161 | + ClassTypeParamList |
| 162 | + : <Head: ClassTypeParam> [ "," <Tail: ClassTypeParamList> ] |
| 163 | + |
| 164 | + ClassTypeParam |
| 165 | + : ClassType |
| 166 | + | <Placeholder: STRING> |
| 167 | + | "?" [ <WildcardBounds: Bounds> ] |
| 168 | + |
| 169 | + Bounds |
| 170 | + : "extends" <Parent: ClassTypeInstance> |
| 171 | + | "super" <Child: ClassTypeInstance> |
| 172 | + |
| 173 | +## Examples |
| 174 | +### The java.lang.Object class |
| 175 | +The [java.lang.Object](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html) |
| 176 | +class can be described in the DSL as follows: |
| 177 | + |
| 178 | + class Object |
| 179 | + constructor Object() |
| 180 | + |
| 181 | + final method getClass() returns Class<?> |
| 182 | + method hashCode() returns int |
| 183 | + method equals(obj as Object) returns boolean |
| 184 | + protected method clone() returns Object |
| 185 | + method toString() returns String |
| 186 | + final method notify() returns nothing |
| 187 | + final method notifyAll() returns nothing |
| 188 | + final method wait(timeout as long) named waitTimeout returns nothing |
| 189 | + final method wait(timeout as long, nanos as int) named waitNanos returns nothing |
| 190 | + final method wait() returns nothing |
| 191 | + protected method finalize() returns nothing |
| 192 | + end class |
| 193 | + |
| 194 | +### The java.lang.reflect.TypeVariable class |
| 195 | +The [java.lang.reflect.TypeVariable](https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/TypeVariable.html) |
| 196 | +class can be translated into the DSL as follows: |
| 197 | + |
| 198 | + interface TypeVariable<D> inherits Type |
| 199 | + method getBounds() returns Type[] |
| 200 | + method getGenericDeclaration() returns D |
| 201 | + method getName() returns String |
| 202 | + end interface |
| 203 | + |
| 204 | +### The android.os.BatteryManager class |
| 205 | +The charging/discharging functionality of the [android.os.BatteryManager](https://developer.android.com/reference/android/os/BatteryManager.html) |
| 206 | +class can be translated as follows: |
| 207 | + |
| 208 | + class BatteryManager |
| 209 | + constant ACTION_CHARGING as String is "android.os.action.CHARGING" |
| 210 | + constant ACTION_DISCHARGING as String is "android.os.action.DISCHARGING" |
| 211 | + method isCharging() returns boolean |
| 212 | + end class |
0 commit comments