I have a table in which I save an ID and a rule like:
| ID | Rule |
|------|--------------------------------------|
| 1 | firstname[0]+'.'+lastname+'#'+domain |
| 2 | firstname+'_'+lastname+'#'+domain |
| 3 | lastname[0]+firstname+'#'+domain |
My problem is: How can I get and analyze/execute that rule in my code? Because the cell is taken as a string and I don't know how to apply that rule to my variables or my code.
I was thinking about String.Format, but I don't know how to split a string taking just the first character with it.
If you could give me an advice or any better way to do this, I'd appreciate that because I'm completely lost.
If that is C#, you could construct a LINQ Expression out of the parse tree from for example ANTLR, or if the format is very simple, regex.
You have to make these steps:
Evaluate the incoming string using ANTLR. You could start off with the C# grammar;
Build an expression from it;
Run the expression giving the firstname, domain, etc. parameters.
Not sure that would do the trick, but you might want to look at CSharpCodeProvier. Never used it, but according to the examples, it seems to be capable of compiling code entered in a textbox.
The thing is that this solution generates an exe file that will be stored in your project folder. Even if you delete them after a successful compiling, that might not be the best option.
Related
I am making an interpreter in Antlr and I am having trouble with the following:
One of my rules states:
expression
: { .. }
| {... }
| identifier
| expression DOT expressioon
| get_index
| invoke_method
| { ... }
;
I thought this way I'd evaluate expressions such as "a.b[2].Run()" or whatever... the idea is being able to use both variables, indexing, and methods. All 3 syntaxes are in place... How do I chain them tho? Is my approach correct? or is there a better way? (The example I provided just randomly throws those in with no specification for the sake of clearance, it's not the actual grammar but I can assure you all other 3 grammar rules(identifier, get_index and invoke_method) are defined and are proper, identifier is a single one, not chained)
I'm doing an basic CSV import/export in C#. Most of it is really simple and basic, we just have one speciality.
In values we import/export, we have some specials values, which are not ASCII values. To ease the work of our end users, the customer decided to convert some values in some other values and do the opposite when importing.
Some examples
Value in our application | ValueS that must be accepted on parsing
-----------------------------------------------------------------------
³ | 3, ^3, **3
μ | u
₃ | 3
⁹ | 9
° | deg
φ | phi
To export, it's easy, we replace the matching character by the first on the second column.
But for the parsing, it's more complicated, and I don't see an easy way to get all the possible values to import?
One example:
H³ 3° (asd)₃
Would be exported as
H3 3deg (asd)3
So to parse this correctly, I've to get all the possibilities:
H3 3deg (asd)3 //This may be a real values
H³ 3deg (asd)3
H₃ 3deg (asd)3
H3 ³deg (asd)3
....
What would be the good way of doing this?
I doubt it's possible with such an encoding. All H3 values are equally likely unless there is a rule that differentiates them. This makes parsing more difficult, not less.
What you are trying to do though looks a lot like what has already been done with tools like Latex or even Word. You should probably use the encodings used by Latex since they've already done the work of encoding symbols to human readable and editable keywords that can be parsed easily, eg: use ^ for power, _ for indices, \degree for degrees, etc.
In fact, even Word allows these same keywords nowadays in the Math editor, allowing you to type \sum and get ∑, or \oint for ∮
You should probably tag the fields that contain substitutions, eg by surrounding them in multiple braces, so that users can use the keywords in their own text.
I think you need to exclude ambiguous mappings. E.g.:
³ | ^3, **3
₃ | 3
⁹ | ^9, **9
or
³ | 3, ^3, **3
₃ | _3
⁹ | 9
ASCII has 7 Bits for each character. Now you want to use chars which are implemented in the space of 8 Bits (UTF8 for example).
Now you lose information by converting your UTF8 character to ASCII but you want get back the full information.
To manage this, you need a mask, which helps to recognize the right character.
You could use special characters as your mask. So you don't reinvent the wheel and others can find the documentation all over the internet for your interface.
But if you make ³ => 3, you lose information (3 superscript => 3; where is the superscript and how you should guess the right choice?)
i have written a grammar for while in ANTLRWorks 1.5.2.
i also added some actions so when i debug my code with a while code it will show 3 address code in output of ANTLRWorks.
my grammar is like that:
NAME:
LETTER (LETTER | DIGIT | '_')*;
NUMBER:
DIGIT+; // just integers
fragment DIGIT:
'0'..'9';
fragment LETTER:
'A'..'Z' | 'a'..'z';
RELATION:
'<' | '<=' | '==' | '>=' | '>' | '!=' ;
WHITESPACE:
(' ' | '\t' | '\r' | '\n')+ { $channel = HIDDEN; };
and i generate my grammar and i have whileParser.cs and whileLexer.cs in output folder.
now i want to add my grammar to a c# project.
i want to get input from user and then show output of my grammar to them.
and i dont know how to add .g file and output classes to a c# project.
i am using visual studio 2013.
can anybody help me?
You grammar contains Java code blocks, you need to translate them to C# first. Actually, it may be a good opportunity for you to use ANTLR 4 instead and/or to switch to a parse tree approach. I should mention there's an ANTLRWorks 2 version, mostly for ANTLR 4, should you need it.
Anyway, just install the ANTLR Visual Studio Plugin and let it handle that for you. It works with both ANTLR 3 and 4.
You'll then have to add the ANTLR runtime to your project. For this, you can install the ANTLR4 NuGet or the ANTLR3 version depending on what version you chose to use in the end.
I just starting to work with SpecFlow and really like the tool. However I am running across some issues in relation to example data inputs into the Scenario Outlines.
Just wondering if what I am facing is normal or whether there is a trick to it.
I am using C# Visual Studio 2013 and writing an MVC App using the underscore style of step definition. I have also tried the regular expression style but still get similar issues.
So the issue is I am providing username, password etc as parameters and including sample data in my Examples. It appears that the following occurs: -
I have to put "" around the parameters when 1st generating the scenario, otherwise it does not get picked up as a parameter at all. However when passing data in from the examples I get a "/" at the end of the data passed in. When I go back to the scenario I then remove the "" around the parameter. This is a little frustrating but if that is the best way to handle it I can live with that. Just wondering if anyone has any advice on this point.
The next issue is related to the data itself. It appears if I have any characters such as # or & etc in my data, then it splits that data at that point and feeds it to the next parameter so I get incorrect data being fed through.
I have included my code below - if anyone has any suggestions or resources to look at that would be appreciated.
Feature File
Feature: AccountRegistration
In order to use Mojito services in my organisation
As a guest user
I want to create an account with administration privelages
Scenario Outline: Register with valid details
Given I am on the registration page
And I have completed the form with <email> <organisation> <password> and <passwordConfirmation>
When I have clicked on the register button
Then I will be logged in as <username>
And my account will be assigned the role of <role>
Examples:
| email | organisation | password | passwordConfirmation | username | role |
| usernamea | Bytes | password1 | password1 | usernamea | Admin |
| usernameb | Bytes | password2 | password2 | usernameb | Admin |
| usernamec | Bytes | password3 | password3 | usernamec | Admin |
| usernamed | Bytes | password4 | password4 | usernamed | Admin |
| usernamee | Bytes | password5 | password5 | usernamee | Admin |
Scenario Outline: Register with invalid details
Given I am on the registration page
And I have completed the form with <email> <organisation> <password> and <passwordConfirmation>
When I have clicked on the register button
Then I will get an error message
Examples:
| email | organisation | password | passwordConfirmation |
| Jonesa#mojito.com | Bytes | 1LTIuta&Sc | wrongpassword |
| Jonesb#mojito.com | Bytes | 1LTIuta&Sc | 1LTIuta&Sc |
| Jonesc#mojito.com | No Organisation | 1LTIuta&Sc | 1LTIuta&Sc |
Steps Generated File
[Binding]
public class AccountRegistrationSteps
{
[Given]
public void Given_I_am_on_the_registration_page()
{
ScenarioContext.Current.Pending();
}
[Given]
public void Given_I_have_completed_the_form_with_usernamea_Bytes_password_P0_and_password_P1(int p0, int p1)
{
ScenarioContext.Current.Pending();
}
[Given]
public void Given_I_have_completed_the_form_with_Jonesa_mojito_com_Bytes_P0_LTIuta_Sc_and_wrongpassword(int p0)
{
ScenarioContext.Current.Pending();
}
[When]
public void When_I_have_clicked_on_the_register_button()
{
ScenarioContext.Current.Pending();
}
[Then]
public void Then_I_will_be_logged_in_as_usernamea()
{
ScenarioContext.Current.Pending();
}
[Then]
public void Then_my_account_will_be_assigned_the_role_of_Admin()
{
ScenarioContext.Current.Pending();
}
[Then]
public void Then_I_will_get_an_error_message()
{
ScenarioContext.Current.Pending();
}
}
SpecFlow does handle string parameters by default, the problem is that you left control up to SpecFlow in determining at runtime what your values are.
When you ran "Generate Step Definitions," you selected "Method name - underscores" in the Style dropdown. This left interpreting the input parameters up to SpecFlow, which will create what are called 'greedy' regular expressions to identify the parameter values. This means that it would include the comma as part of the value.
Had you selected "Regular expressions in attributes," (or refactored your code a touch and decorated your attributes by hand) your step could look like this:
[Given(#"I have completed the form with (.*), (.*), (.*), and (.*)")]
public void Given_I_have_completed_the_form_with(string email, string org, string pwd, string conf)
{
//do stuff here
}
This creates a more 'parsimonious' expression that tells SpecFlow to accept strings of any length, up to but not including any trailing commas. Single quotes around the regular expressions would make it even more explicit:
[Given(#"I have completed the form with '(.*)', '(.*)', '(.*)', and '(.*)'")]
Managing the regular expressions yourself can create headaches, but it really exposes the full power of SpecFlow if you do so.
RESOLVED - It was not an issue with the use of characters such as # or &. It was actually using commas in my Given Statement. I found if I used 'and' it works. So to get it working the statement had to be written as below: -
SOLUTION
Write statement as
Given I have completed the form with <email> and <organisation> and <password> and <passwordConfirmation>
Modify statement to put single quotes around paramaters that need to be strings
Given I have completed the form with '<email>' and '<organisation>' and '<password>' and '<passwordConfirmation>'
Generation Step Definitions and then change statement back to exclude single quotes
Given I have completed the form with <email> and <organisation> and <password> and <passwordConfirmation>
A bit of mucking around but it gets the correct results. Hopefully in the future SpecFlow will be updated to handle paramaters as strings as default.
For future reference, if Cater's answer doesn't do the job. I had the following problem
Give I have a <typeOfDay> day
When Im asked how I am
Then I will say <feeling>
Example:
|typeOfDay|feeeling|
|good |happy |
|bad |sad |
You'll notice that "feeling" in the Then statement won't be able to find a corresponding value because of a typo. This causes SpecFlow to throw the "Input string not in a correct format" error. Which in my case took an embarrassingly long time to find.
Something else to check :)
I use CSharp, XPath and HTMLAgility Pack. I use XPath strings such as:
"//table[3]/td[1]/span[2]/text() | //table[6]/td[1]/span[2]/text()"
"//table[8]/td[1]/span[2]/text() | //table[10]/td[1]/span[2]/text()"
The difference is only in table numbers. Is it possible to use some other XPath function to replace the XPath or |?
What I actually do: With the first XPath string (where I have table numbers 3 & 6) I extract one value. With the second XPath string (where i have table numbers are 8 & 10) I extract another value.
And additional question about performance - is the XPath string //table[8]/td[1]/span[2]/text() faster than the XPath string with OR //table[8]/td[1]/span[2]/text() | //table[10]/td[1]/span[2]/text()? I ask this because I have many many XPath strings for many many values and if there is a difference which really means I need to try something else. I can't do the measurement right now that's why I ask you this question to share your experience.
Firstly, //table[6] looks odd. Are you sure you don't mean (//table)[6]? (The first selects every table that is the 6th child of its parent; the second selects the sixth table in the document.) I will assume the latter.
In XPath 2.0 you can write
(//table)[position()=(3,6,8,10)]/td[1]/span[2]/text()
In 1.0 that would have to be
(//table)[position()=3 or position()=6 or position()=8 or position()=10]
/td[1]/span[2]/text()
Or (in either release) you could write
((//table)[3] | (//table)[6] | (//table)[8] | (//table)[10])/td[1]/span[2]/text()
Your question about performance can't be answered without knowing what XPath implementation you are using.