For In Grammar Breaking Identifiers - syntax-highlighting

I'm trying to make a syntax highlighter in Atom for a toy language I'm working on. I'm at the stage where I'm defining the context free grammar. I've been building it up step by step and writing tests along the way. When I added the grammar for a for in loop it broke my test for parsing identifiers because the identifier started with "in". Here's the grammar as it stands now (Sorry for pasting so much code but I didn't know what might be relevant so I just added the whole thing):
module.exports = grammar({
name: 'MooLang',
rules: {
source_file: $ => repeat($._declaration),
_declaration: $ => choice(
$.variable_declaration,
$._statement
),
variable_declaration: $ => seq(
choice('var', 'let'),
$.identifier,
optional(seq(
':', $._type
)),
optional(seq(
'=', $._expression
)),
$.eol
),
_statement: $ => choice(
$.for_statement,
$.expression_statement
),
for_statement: $ => prec(0, seq(
'for',
'(',
choice(
$.variable_declaration,
$.expression_statement,
),
'in',
$._expression,
')',
$._statement
)),
expression_statement: $ => prec(1, seq(
$._expression,
$.eol
)),
_expression: $ => choice(
$.assignment,
$.comparison_expression,
$.addition_expression,
$.multiplication_expression,
$.unary_expression,
prec(5, $.primary),
prec(-1, $._type) // TODO:(Casey) Remove this
),
assignment: $ => prec.right(0, seq(
$.identifier,
'=',
$._expression
)),
comparison_expression: $ => prec.left(1, seq(
$._expression,
choice('<', '<=', '>', '>=', '==', '!='),
$._expression
)),
addition_expression: $ => prec.left(2, seq(
$._expression,
choice('+', '-'),
$._expression
)),
multiplication_expression: $ => prec.left(3, seq(
$._expression,
choice('*', '/', '%'),
$._expression
)),
unary_expression: $=> prec.right(4, seq(
choice('!', '-'),
$.primary
)),
_type: $ => choice(
$.primitive_type,
$.list_type,
$.map_type
),
primitive_type: $ => choice(
'bool', 'string',
'int8', 'int16', 'int32', 'int64',
'uint8', 'uint16', 'uint32', 'uint64',
'float32', 'float64'
),
list_type: $ => seq(
'[',
$._type,
']'
),
map_type: $ => seq(
'{',
$._type,
':',
$._type,
'}'
),
primary: $ => choice(
$.bool_literal,
$.list_literal,
$.map_literal,
$.parenthetical_expression,
$.identifier,
$.number
),
bool_literal: $ => choice('true', 'false'),
list_literal: $ => seq(
'[',
optional(seq(
$._expression,
repeat(seq(
',',
$._expression
)),
optional(','),
)),
']'
),
map_literal: $ => seq(
'{',
optional(seq(
$._expression,
':',
$._expression,
repeat(seq(
',',
$._expression,
':',
$._expression,
)),
)),
'}'
),
parenthetical_expression: $ => seq(
'(',
$._expression,
')'
),
identifier: $ => prec(99, /[a-zA-Z_][a-zA-Z0-9_]*/),
number: $ => prec(99, /\d+(_\d+)*(\.\d+)?/),
eol: $ => '\n'
}
});
Here are the relevant tests:
==================
Identifier Tests
==================
13india
---
(source_file
(expression_statement (primary (number)) (MISSING))
(expression_statement (primary (identifier)) (eol))
)
==================
For Tests
==================
for (var a in people) a + 1
---
(source_file
(for_statement (variable_declaration (identifier)) (primary (identifier)) (expression_statement (addition_expression (primary (identifier)) (primary (number))) (eol)))
)
Until I added the grammar for the for loop all of the Identifier Tests were passing but now I get this output:
My guess is that it finds an unexpected 'd' because it thinks this is the 'in' keyword. But I can't figure out why it would think that since it doesn't match anything else about the for loop.

Related

laravel custom validation issue it is not working proper way some error is given and some is not given

this is my validation code but not given proper validation error.It is showing only these error
Special character is not allowed like \t, :, \n
GivenName is required
MiddleName is required
while maximum field is required
$this->validate($request,[
'DisplayName'=>'required|max:500',
'DisplayName'=>'regex:/(^[A-Za-z0-9]+$)+/',
'GivenName' => 'required|max:100',
'MiddleName' =>'required|max:100',
'Title' => 'max:16',
'Suffix' =>'max:16',
'FamilyName' =>'max:100'
],
[
'DisplayName.required' => 'DisplayName is required!',
'DisplayName.regex' => 'Special character is not allowed like \t, :, \n ',
'GivenName.max' =>'GivenName is max 100 words',
'GivenName.required' =>'GivenName is required',
'MiddleName.max' =>'MiddleName is max 100 words',
'MiddleName.required' =>'MiddleName is required',
'Title.max' =>'Title is max 16 words',
'Suffix.max' =>'Suffix is max 16 words',
'FamilyName.max' =>'FamilyName is 100 words'
],
When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character, Like :
$this->validate(request(), [
'userName' =>
array(
'required',
'regex:/(^([a-zA-Z]+)(\d+)?$)/u'
)
]);
So your code should be :
$this->validate($request,[
'DisplayName' =>
array(
'required',
'max:500',
'regex:/(^[A-Za-z0-9]+$)+/'
),
'GivenName' => 'required|max:100',
'MiddleName' =>'required|max:100',
'Title' => 'max:16',
'Suffix' =>'max:16',
'FamilyName' =>'max:100'
],
[
'DisplayName.required' => 'DisplayName is required!',
'DisplayName.regex' => 'Special character is not allowed like \t, :, \n ',
'GivenName.max' =>'GivenName is max 100 words',
'GivenName.required' =>'GivenName is required',
'MiddleName.max' =>'MiddleName is max 100 words',
'MiddleName.required' =>'MiddleName is required',
'Title.max' =>'Title is max 16 words',
'Suffix.max' =>'Suffix is max 16 words',
'FamilyName.max' =>'FamilyName is 100 words'
],
);

A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: mysql/mysql_driver.php Line Number: 552

Model :
function trans_gdc_add()
{
$trans_gdc_gc_package[0] = $this->input->post('trans_gdc_gc_package');
$trans_gdc_gc_package1 = implode(",", $trans_gdc_gc_package[0]);
$add=array(
'trans_gdc_no' => $this->input->post('trans_gdc_no'),
'trans_gdc_to' => $this->input->post('trans_gdc_to'),
'trans_gdc_vehicle_no' => $this->input->post('trans_gdc_vehicle_no'),
'trans_gdc_vehicle_type' => $this->input->post('trans_gdc_vehicle_type'),
'trans_gdc_vehicle_chas' => $this->input->post('trans_gdc_vehicle_chas'),
'trans_gdc_vehicle_make' => $this->input->post('trans_gdc_vehicle_make'),
'trans_gdc_vehicle_eng' => $this->input->post('trans_gdc_vehicle_eng'),
'trans_gdc_vehicle_permit' => $this->input->post('trans_gdc_vehicle_permit'),
'trans_gdc_vehicle_pol' => $this->input->post('trans_gdc_vehicle_pol'),
'trans_gdc_vehicle_isby' => $this->input->post('trans_gdc_vehicle_isby'),
' trans_gdc_date' => $this->input->post(' trans_gdc_date'),
'trans_gdc_from' => $this->input->post('trans_gdc_from'),
'trans_gdc_driver_add' => $this->input->post('trans_gdc_driver_add'),
'trans_gdc_lic_no' => $this->input->post('trans_gdc_lic_no'),
'trans_gdc_vehilce_owner_name' => $this->input->post('trans_gdc_vehilce_owner_name'),
'trans_gdc_vehicle_owner_add' => $this->input->post('trans_gdc_vehicle_owner_add'),
'trans_gdc_vehicle_owner_mob' => $this->input->post('trans_gdc_vehicle_owner_mob'),
'trans_gdc_broker_name' => $this->input->post('trans_gdc_broker_name'),
'trans_gdc_broker_mob' => $this->input->post('trans_gdc_broker_mob'),
'trans_gdc_gc_no' => $this->input->post('trans_gdc_gc_no'),
'trans_gdc_gc_package' => $trans_gdc_gc_package1,
'trans_gdc_gc_cont' => $this->input->post('trans_gdc_gc_cont'),
'trans_gdc_gc_weight' => $this->input->post('trans_gdc_gc_weight'),
'trans_gdc_gc_freight' => $this->input->post('trans_gdc_gc_freight'),
'trans_gdc_gc_consignor' => $this->input->post('trans_gdc_gc_consignor'),
'trans_gdc_gc_consignee' => $this->input->post('trans_gdc_gc_consignee'),
'status' => '1'
);
return $this->db->insert('trans_gc_add',$add);
}
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: mysql/mysql_driver.php
Line Number: 552
Please help me to resolve this issue .
check this
' trans_gdc_date' => $this->input->post(' trans_gdc_date'),
space might be giving the problem, repla
'trans_gdc_date' => $this->input->post(' trans_gdc_date'),
You can't directly merge strings in arrays!
your trying to merge concatinate strings with arrays or lets say arrays with string thats not possible
General Example
<?php
$stack = array("Orange", "Banane");
array_push($stack, "Apfel", "Himbeere");
print_r($stack);
?>
for your code Example
// array push tiggers a php array as stack!
$add=array()
array_push($add, 'trans_gdc_no', $this->input->post('trans_gdc_no'));
array_push($add, 'trans_gdc_to', $this->input->post('trans_gdc_to'));
print_r($add);

Zend Framework Validate field is integer between 1 and 5

I use Zend\InputFilter\InputFilter class for form validation. point field should accept only integers between 1 and 5. But it doesn't work properly it accepts string starting with integers between 1 and 5. For example 1kjhkjh, 2tgfjhgfjhf, 4jhkljg...
What is wrong in my code?
$inputFilter->add (
$inputFilter->getFactory()->createInput (
array (
'name' => 'point',
'required' => true,
'validators' => array (
array(
'name' => 'Digits'),
array (
'name' => 'Between',
'options' => array (
'min' => 1,
'max' => 5,
'messages' => array('notBetween' => 'Point must be between %min% and %max%')
)
)
)
)
)
);
Use the the second parameter of zend validator to break the validators chain and return an error, breakChainOnFailure (documentation) tells the element to stop validating if this error is triggered, so in your case if it is not a digit the user gets an error, when the user has fixed the error the second validator will get triggered too:
$inputFilter->add (
$inputFilter->getFactory()->createInput (
array (
'name' => 'point',
'required' => true,
'validators' => array (
array(
'name' => 'Digits', 'breakChainOnFailure' => true),
array (
'name' => 'Between',
'options' => array (
'min' => 1,
'max' => 5,
'messages' => array('notBetween' => 'Point must be between %min% and %max%')
)
)
)
)
)
);
Another option would be to replace Zend_Validate_Digits with Zend_Validate_Int (docmentation) depending of what error message you prefer to give to the user if he enters non valid data. Of course as suggested in the comments you could also use the Zend_Filter_Int if what you want is to handle any non valid data by fixing it yourself and do not give the user feedback about what he did wrong.
'breakChainOnFailure': true
should be
'breakChainOnFailure'=> true

using get_metric_statistics in simple_aws using ruby

I am trying to explore the simple_aws gem. When I connect to cloudwatch to get the metric statistics I get an error as follows:
cw.get_metric_statistics(
:metric_name => metric_name,
:period => period,
:start_time => start_time,
:end_time => end_time,
:statistics => "Average",
:namespace => "AWS/EC2"
)
SimpleAWS::UnsuccessfulResponse: MissingParameter (400):
The parameter Namespace is required.
The parameter MetricName is required.
The parameter StartTime is required.
The parameter EndTime is required.
The parameter Period is required.
The parameter Statistics is required.
Later, I tried this:
cw.get_metric_statistics(
options => [
{:metric_name=>"CPUUtilization",
:period=>60,
:start_time => Time.now()-86400,
:end_time => Time.now()-3600,
:statistics => "Average"
}
]
)
But got the following error:
URI::InvalidComponentError: bad component(expected query component):
Action=GetMetricStatistics&{:metric_name=>"CPUUtilization"}.1.metric_name=CPUUtilization&{:metric_name=>"CPUUtilization"}.1.period=60&{:metric_name=>"CPUUtilization"}.1.start_time=2012-05-06%2014%3A25%3A28%20%2B0530&{:metric_name=>"CPUUtilization"}.1.end_time=2012-05-07%2013%3A25%3A28%20%2B0530&{:metric_name=>"CPUUtilization"}.1.statistics=Average&AWSAccessKeyId=AccessKey&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-05-07T08%3A55%3A28Z&Version=2010-08-01&Signature=Signature
one more try:
cw.get_metric_statistics(
namespace: 'AWS/EC2',
measure_name: 'CPUUtilization',
statistics: 'Average',
start_time: time-1000,
dimensions: "InstanceId=#{instance_id}"
)
ArgumentError: comparison of Array with Array failed
Can anybody please help find the correct syntax for issuing this command.
result = cw.get_metric_statistics(step,
start_time,
end_time,
metric,
'AWS/RDS',
'Average',
dimensions={'DBInstanceIdentifier': [indentifier]})
This also worked for me
I found that this works;
lat = cw.get_metric_statistics(
'MetricName' => 'Latency',
'Period' => 60,
'StartTime' => (Time.now() - 3600).iso8601,
'EndTime' => Time.now().iso8601,
'Statistics.member.1' => "Average",
'Namespace' => "AWS/ELB",
'Unit' => 'Seconds'
)
Firstly that the datetime is required in ISO8601 format, secondly that the parameters need to be cased correctly, thirdly the Unit parameter is required, and finally that Statistics needed a namespace(?) after it.
Hope this helps, even if it is a little late.

cakephp error: unexpected '(', expecting ')', is my syntax wrong?

Do you see any syntax errors here?
'Coupon'=>array(
'fields'=>array(
'promo_code','desc'
),
'conditions'=>array(
'OR'=>array(
'expires' =>0,
'Coupon.end_date >'=>date('Y-m-d')
)
)
),
This is part of my 'contain' array in my controller code. When I remove this snippet from my code, cake works great (only I need this part!). Am posting the entire statement below. Help?
public $paginate = array(
'Location'=>array(
'joins' => array(
array(
'table' => 'locations_tags',
'alias' => 'LocationsTag',
'type' => 'inner',
'conditions'=> array(
'LocationsTag.location_id = Location.id'
)
)
),
'limit'=>9,
'contain'=>array(
'Course'=>array(
'fields'=>array(
'specials', 'contact','desc'
),
'conditions'=>array('Course.active'=>1)
),
'Charter'=>array(
'fields'=>array(
'book','specials', 'contact','desc'
),
'conditions'=>array('Charter.active'=>1)
),
'Restaurant'=>array(
'fields'=>array(
'menu','wine_list','specials', 'contact','desc'
),
'conditions'=>array('Restaurant.active'=>1)
),
'Nightclub'=>array(
'fields'=>array(
'menu','schedule','specials', 'contact','desc'
),
'conditions'=>array('Nightclub.active'=>1)
),
'Store'=>array(
'fields'=>array(
'catalog','specials', 'contact','desc'
),
'conditions'=>array('Store.active'=>1)
),
'Coupon'=>array(
'fields'=>array(
'promo_code','desc'
),
'conditions'=>array(
'OR'=>array(
'expires' =>0,
'Coupon.end_date >'=>date('Y-m-d')
)
)
),
'Image',
'Tag'=>array(
'fields'=>array(
'seo_tag'
)
)
)
)
);
'Coupon.end_date >'=>date('Y-m-d')
You can't assign calculated values (e.g., calling functions) in class property declarations. They have to be constant values.
For a calculated value, you'd have to assign that in the constructor or something.
From the docs:
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

Resources