Skip to content

Commit

Permalink
Adding float attribute type to XHP.
Browse files Browse the repository at this point in the history
Summary:
Add a new type for attributes in XHP for float (AKA double).

Reviewed By: marcel

Test Plan:
Added a new test for float attributes.
  • Loading branch information
Nora Mullaney committed Mar 9, 2010
1 parent e1d699f commit 123d916
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions php-lib/core.php
Expand Up @@ -71,6 +71,7 @@ abstract class :x:composable-element extends :x:base {
const TYPE_OBJECT = 5;
const TYPE_VAR = 6;
const TYPE_ENUM = 7;
const TYPE_FLOAT = 8;

protected function init() {}

Expand Down Expand Up @@ -300,6 +301,12 @@ final protected function validateAttributeValue($attr, &$val) {
}
return;

case self::TYPE_FLOAT:
if (!is_numeric($val)) {
$val = (float)$val;
}
return;

case self::TYPE_ARRAY:
if (!is_array($val)) {
throw new XHPInvalidAttributeException($this, 'array', $attr, $val);
Expand Down
11 changes: 11 additions & 0 deletions tests/attr-float.phpt
@@ -0,0 +1,11 @@
--TEST--
Float attribute parsing
--FILE--
<?php
class :foo {
attribute
float b;
}
echo "pass";
--EXPECT--
pass
4 changes: 4 additions & 0 deletions xhp/parser.y
Expand Up @@ -190,6 +190,7 @@ static void replacestr(string &source, const string &find, const string &rep) {
%token T_XHP_ARRAY
%token T_XHP_STRING
%token T_XHP_ENUM
%token T_XHP_FLOAT
%token T_XHP_REQUIRED

%%
Expand Down Expand Up @@ -1749,6 +1750,9 @@ xhp_attribute_decl_type:
| T_XHP_ENUM '{' { push_state(PHP); } xhp_attribute_enum { pop_state(); } '}' {
$$ = "7, array(" + $4 + ")";
}
| T_XHP_FLOAT {
$$ = "8, null";
}
;

xhp_attribute_enum:
Expand Down
1 change: 1 addition & 0 deletions xhp/scanner.l
Expand Up @@ -133,6 +133,7 @@ NEWLINE ("\r\n"|"\n"|"\r")
<XHP_ATTR_TYPE_DECL>{
bool tok(T_XHP_BOOLEAN);
int tok(T_XHP_NUMBER);
float tok(T_XHP_FLOAT);
var tok(T_VAR);
array tok(T_XHP_ARRAY);
string tok(T_XHP_STRING);
Expand Down

0 comments on commit 123d916

Please sign in to comment.