src: use the new module annotation in exising modules
authorJean Privat <jean@pryen.org>
Sat, 19 Jul 2014 03:20:53 +0000 (23:20 -0400)
committerJean Privat <jean@pryen.org>
Sat, 19 Jul 2014 03:21:32 +0000 (23:21 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/android_annotations.nit
src/cached.nit
src/common_ffi/c_compiler_options.nit
src/common_ffi/extra_java_files.nit
src/common_ffi/pkgconfig.nit
src/platform.nit

index 39037e2..2183269 100644 (file)
@@ -23,6 +23,7 @@ import modelbuilder
 import modelize_property
 import literal
 import typing
+private import annotation
 
 # Metadata associated to an Android project
 class AndroidProject
@@ -84,10 +85,10 @@ redef class ModelBuilder
                for an in annots do project.target_sdk = an.arg_as_int(self)
 
                annots = collect_annotations_on_modules("android_manifest", mmodule)
-               for an in annots do project.manifest_lines.add an.arg_as_string(self)
+               for an in annots do project.manifest_lines.add an.arg_as_string(self) or else ""
 
                annots = collect_annotations_on_modules("android_manifest_application", mmodule)
-               for an in annots do project.manifest_application_lines.add an.arg_as_string(self)
+               for an in annots do project.manifest_application_lines.add an.arg_as_string(self) or else ""
 
                # Get the date and time (down to the minute) as string
                var local_time = new Tm.localtime
@@ -151,62 +152,6 @@ redef class ModelBuilder
 end
 
 redef class AAnnotation
-       # Get the single argument of `self` as a `String`. Raise error on any inconsistency.
-       private fun arg_as_string(modelbuilder: ModelBuilder): String
-       do
-               var annotation_name = n_atid.n_id.text
-               var format_error = "Annotation error: \"{annotation_name}\" expects a single String as argument."
-
-               var args = n_args
-               var platform_name
-               if args.length != 1 then
-                       modelbuilder.error(self, format_error)
-                       return ""
-               else
-                       var arg = args.first
-                       
-                       if not arg isa AExprAtArg then
-                               modelbuilder.error(self, format_error)
-                               return ""
-                       end
-
-                       var expr = arg.n_expr
-                       if not expr isa AStringFormExpr then
-                               modelbuilder.error(self, format_error)
-                               return ""
-                       end
-                       return expr.value.as(not null)
-               end
-       end
-
-       # Get the single argument of `self` as an `Int`. Raise error on any inconsistency.
-       private fun arg_as_int(modelbuilder: ModelBuilder): nullable Int
-       do
-               var annotation_name = n_atid.n_id.text
-               var format_error = "Annotation error: \"{annotation_name}\" expects a single Int as argument."
-
-               var args = n_args
-               var platform_name
-               if args.length != 1 then
-                       modelbuilder.error(self, format_error)
-                       return null
-               else
-                       var arg = args.first
-                       
-                       if not arg isa AExprAtArg then
-                               modelbuilder.error(self, format_error)
-                               return null
-                       end
-
-                       var expr = arg.n_expr
-                       if not expr isa AIntExpr then
-                               modelbuilder.error(self, format_error)
-                               return null
-                       end
-                       return expr.value.as(not null)
-               end
-       end
-
        # Returns a version string (example: "1.5.6b42a7c") from an annotation `version(1, 5, git_revision)`.
        #
        # The user can enter as many fields as needed. The call to `git_revision` will be replaced by the short
@@ -219,33 +164,26 @@ redef class AAnnotation
                var args = n_args
                var platform_name
                if args.length < 1 then
-                       modelbuilder.error(self, "Annotation error: \"{annotation_name}\" expects at least a single argument.")
+                       modelbuilder.error(self, "Annotation error: \"{name}\" expects at least a single argument.")
                        return ""
                else
                        for arg in args do
-                               var format_error = "Annotation error: \"{annotation_name}\" expects its arguments to be of type Int or a call to `git_revision`"
+                               var format_error = "Annotation error: \"{name}\" expects its arguments to be of type Int or a call to `git_revision`"
                                
-                               if not arg isa AExprAtArg then
-                                       modelbuilder.error(self, format_error)
-                                       return ""
+                               var value
+                               value = arg.as_int
+                               if value != null then
+                                       version_fields.add value
+                                       continue
                                end
 
-                               var expr = arg.n_expr
-                               if expr isa AIntExpr then
-                                       var value = expr.value
-                                       assert value != null
+                               value = arg.as_string
+                               if value != null then
                                        version_fields.add value
-                               else if expr isa AStringFormExpr then
-                                       version_fields.add expr.value.as(not null)
-                               else if expr isa ACallExpr then
-                                       # We support calls to "git" only
-                                       var exec_args = expr.n_args.to_a
-                                       if expr.n_id.text != "git_revision" or not exec_args.is_empty then
-                                               modelbuilder.error(self,
-                                                       "Annotation error: \"{annotation_name}\" accepts only calls to `git_revision` with the command as arguments.")
-                                               return ""
-                                       end
+                               end
 
+                               value = arg.as_id
+                               if value == "git_revision" then
                                        # Get Git short revision
                                        var proc = new IProcess("git", "rev-parse", "--short", "HEAD")
                                        proc.wait
@@ -261,10 +199,11 @@ redef class AAnnotation
                                        if dirty then revision += ".d"
 
                                        version_fields.add revision
-                               else
-                                       modelbuilder.error(self, format_error)
-                                       return ""
+                                       continue
                                end
+
+                               modelbuilder.error(self, format_error)
+                               return ""
                        end
                end
 
index 1ca9256..ead285f 100644 (file)
@@ -21,6 +21,7 @@ module cached
 import modelize_property
 import parser_util
 import simple_misc_analysis
+private import annotation
 
 redef class ToolContext
        var cached_phase: Phase = new CachedPhase(self, [modelize_property_phase])
@@ -41,7 +42,7 @@ private class CachedPhase
        redef fun process_annotated_node(npropdef, nat)
        do
                # Skip if we are not interested
-               if nat.n_atid.n_id.text != "cached" then return
+               if nat.name != "cached" then return
 
                # Do some validity checks and print errors if the annotation is used incorrectly
                var modelbuilder = toolcontext.modelbuilder
index ca64759..7717973 100644 (file)
@@ -21,6 +21,7 @@ module c_compiler_options
 
 import c
 import cpp
+private import annotation
 
 redef class ToolContext
        var c_compiler_options_phase: Phase = new CCompilerOptionsPhase(self, null)
@@ -36,7 +37,7 @@ private class CCompilerOptionsPhase
        redef fun process_annotated_node(nmoduledecl, nat)
        do
                # Skip if we are not interested
-               var annotation_name = nat.n_atid.n_id.text
+               var annotation_name = nat.name
                if annotation_name != compiler_annotation_name and
                   annotation_name != linker_annotation_name and
                   annotation_name != cpp_compiler_annotation_name then return
index 632a322..67a5f9d 100644 (file)
@@ -21,6 +21,7 @@ module extra_java_files
 
 import literal
 import java
+private import annotation
 
 redef class ToolContext
        var extra_java_files_phase: Phase = new JavaExtraFilesPhase(self, [literal_phase])
@@ -38,7 +39,7 @@ private class JavaExtraFilesPhase
        do
                # Skip if we are not interested
                var annot_name = "extra_java_files"
-               if nat.n_atid.n_id.text != annot_name then return
+               if nat.name != annot_name then return
 
                # Do some validity checks and print errors if the annotation is used incorrectly
                var modelbuilder = toolcontext.modelbuilder
@@ -65,20 +66,12 @@ private class JavaExtraFilesPhase
 
                var format_error = "Syntax error: \"{annot_name}\" expects its arguments to be paths to java files."
                for arg in args do
-                       if not arg isa AExprAtArg then
-                               modelbuilder.error(nat, format_error)
+                       var path = arg.as_string
+                       if path == null then
+                               modelbuilder.error(arg, format_error)
                                return
                        end
 
-                       var expr = arg.n_expr
-                       if not expr isa AStringFormExpr then
-                               modelbuilder.error(nat, format_error)
-                               return
-                       end
-
-                       var path = expr.value
-                       assert path != null
-
                        # Append specified path to directory of the Nit source file
                        var source_file = nat.location.file
                        if source_file != null then path = "{source_file.filename.dirname}/{path}"
index b263834..5306b97 100644 (file)
 module pkgconfig
 
 import c
+private import annotation
 
 redef class ToolContext
-       var pkgconfig_phase: Phase = new PkgconfigPhase(self, null)
+       var pkgconfig_phase: Phase = new PkgconfigPhase(self, [literal_phase])
 end
 
 # Detects the `pkgconfig` annotation on the module declaration only.
@@ -31,7 +32,7 @@ class PkgconfigPhase
        redef fun process_annotated_node(nmoduledecl, nat)
        do
                # Skip if we are not interested
-               if nat.n_atid.n_id.text != "pkgconfig" then return
+               if nat.name != "pkgconfig" then return
 
                # Do some validity checks and print errors if the annotation is used incorrectly
                var modelbuilder = toolcontext.modelbuilder
@@ -49,19 +50,12 @@ class PkgconfigPhase
 
                var pkgs = new Array[String]
                for arg in args do
-                       if not arg isa AExprAtArg then
+                       var pkg = arg.as_string
+                       if pkg == null then
                                modelbuilder.error(nat, "Syntax error: \"pkgconfig\" expects its arguments to be the name of the package as String literals.")
                                return
                        end
 
-                       var expr = arg.n_expr
-                       if not expr isa AStringFormExpr then
-                               modelbuilder.error(nat, "Syntax error: \"pkgconfig\" expects its arguments to be the name of the package as String literals.")
-                               return
-                       end
-
-                       var pkg = expr.collect_text
-                       pkg = pkg.substring(1, pkg.length-2)
                        pkgs.add(pkg)
                end
 
index 3c5367f..9e3e92e 100644 (file)
@@ -19,6 +19,7 @@ module platform
 import modelize_property
 import parser_util
 import modelbuilder
+private import annotation
 
 redef class ToolContext
        var platform_phase: Phase = new PlatformPhase(self, [modelize_property_phase])
@@ -37,7 +38,7 @@ private class PlatformPhase
                var annotation_name = "platform"
 
                # Skip if we are not interested
-               if nat.n_atid.n_id.text != annotation_name then return
+               if nat.name != annotation_name then return
 
                # Do some validity checks and print errors if the annotation is used incorrectly
                var modelbuilder = toolcontext.modelbuilder
@@ -54,21 +55,12 @@ private class PlatformPhase
                else if args.is_empty then
                        platform_name = nmoduledecl.n_name.collect_text
                else
-                       var arg = args.first
-                       var format_error = "Syntax error: \"{annotation_name}\" expects its argument to be the name of the target platform as a String literal."
-                       
-                       if not arg isa AExprAtArg then
+                       platform_name = args.first.as_string
+                       if platform_name == null then
+                               var format_error = "Syntax error: \"{annotation_name}\" expects its argument to be the name of the target platform as a String literal."
                                modelbuilder.error(nat, format_error)
                                return
                        end
-
-                       var expr = arg.n_expr
-                       if not expr isa AStringFormExpr then
-                               modelbuilder.error(nat, format_error)
-                               return
-                       end
-                       var target = expr.collect_text
-                       platform_name = target.substring(1, target.length-2)
                end
 
                var nmodule = nmoduledecl.parent.as(AModule)