Ticket #22 (closed defect: fixed)

Opened 1 year ago

Last modified 1 year ago

Rewrite the annotation parser

Reported by: schst Assigned to: mikey
Priority: blocker Milestone:
Component: ReflectionApi Version:
Keywords: Cc:

Description

The annotation parser currently is extremely buggy and needs to be written.

Current issues are:

  • Several chars are not possible in parameter values, like * and ,
  • Annotation values should be type-sensitive, strings should be enclosed in quotes

The new parser should allow annotations like:

/**
 * @MyAnnotation(foo=42,
 *               bar='This is a string, it contains evil chars like * or @')
 * @MyAnnotation(bool=true)
 * @MyAnnotation(key='Any
 *                    Multiline
 *                    Value')
 * @MyAnnotation
 * @MyAnnotation()
 */

Furthermore, it would be nice to have if an annotation value could point to a class:

@MyAnnotation(class=net.stubbles.xml.serializer.stubXMLSerializer.class)

Until this issue is fixed, we should not publish a preview release.

Change History

03/19/07 01:53:27 changed by mikey

What do you mean with "Annotation values should be type-sensitive"?

A basic solution could be a regular expression. However I didn't found an opportunity to include newlines.

<?php
$docComment = 
'/**
 * foobarbaz
 * 
 * more foobarbaz
 * that goes over more than one line
 * evil as we are it contains an * @sign, too
 * 
 * @static
 * @param   Foo  $foo  optional  description
 * @return  blabla
 * @Annotation
 * @Casted[Annotation]
 * @Value(foo)
 * @MoreValues(foo="bar) * @bum/", yeah=\'aha\')
 * @CastedValues[Annotation](bar=baz,
 *                           dum="blubber")
 * @EmptyValue()
 */';
//
$regex   = '/\n\s\* @{1}(\w*)\[?(\w*)\]?(\(?.*)/';
$matches = array();
echo '<pre>';
var_dump(preg_match_all($regex, $docComment, $matches));
var_dump($matches);
?>

Now $matches contains the following: the names of the annotations in key 1:

array(9) {
    [0]=>
    string(6) "static"
    [1]=>
    string(5) "param"
    [2]=>
    string(6) "return"
    [3]=>
    string(10) "Annotation"
    [4]=>
    string(6) "Casted"
    [5]=>
    string(5) "Value"
    [6]=>
    string(10) "MoreValues"
    [7]=>
    string(12) "CastedValues"
    [8]=>
    string(10) "EmptyValue"
  }

The names of the casts in key 2:

[2]=>
  array(9) {
    [0]=>
    string(0) ""
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
    [3]=>
    string(0) ""
    [4]=>
    string(10) "Annotation"
    [5]=>
    string(0) ""
    [6]=>
    string(0) ""
    [7]=>
    string(10) "Annotation"
    [8]=>
    string(0) ""
  }

The value fields in key 3:

[3]=>
  array(9) {
    [0]=>
    string(1) "
"
    [1]=>
    string(36) "   Foo  $foo  optional  description
"
    [2]=>
    string(9) "  blabla
"
    [3]=>
    string(1) "
"
    [4]=>
    string(1) "
"
    [5]=>
    string(6) "(foo)
"
    [6]=>
    string(33) "(foo="bar) * @bum/", yeah='aha')
"
    [7]=>
    string(10) "(bar=baz,
"
    [8]=>
    string(3) "()
"
  }

03/25/07 17:04:53 changed by schst

  • owner changed from mikey to schst.
  • status changed from new to assigned.

I just committed a first version of a parser using a state machine. See [424] for details of the implementation.

This parser still needs finetuning, but it seems like a good direction to take. Maybe we should cache the result somewhere for improved performance.

03/27/07 20:43:47 changed by schst

  • owner changed from schst to mikey.
  • status changed from assigned to new.

03/31/07 20:06:19 changed by schst

  • component changed from core to ReflectionApi.

Changed to correct component

04/14/07 17:10:59 changed by mikey

  • status changed from new to closed.
  • resolution set to fixed.

Fixed with changeset 492.

07/02/07 10:40:45 changed by

  • milestone deleted.

Milestone Version 0.1.0 deleted