Checking the contents of a C # method

I need to check the contents of a C # method.

I do not need syntax errors that do not affect the scope of the method.

I like characters that invalidate the parsing of the rest of the code. For instance:

method() { /* valid comment */ /* <-- bad for (i..) { } for (i..) { <-- bad } 

I need to check / correct any unpaired characters.

This includes / * * /, {}, and possibly others.

How can I do it?

My first thought was Regex, but that obviously wouldn't be done.

+4
source share
5 answers

To get a reasonable answer, you need to study your problem more carefully.

For example, what are you going to do with methods containing preprocessor directives?

 void M() { #if FOO for(foo;bar;blah) { #else while(abc) { #endif Blah(); } } 

This is stupid but legal, so you need to deal with it. Are you going to consider this an inconsistent bracket or not?

Can you provide a detailed specification of exactly what you want to define? As we saw several times on this site, people cannot successfully create a procedure that divides two numbers without a specification. You are talking about analysis, which is much more complicated than dividing two numbers; The code that executes what you describe in a real compiler is tens of thousands of lines.

+3
source

Regular expression, of course, is not the answer to this problem. Regex are useful tools for certain types of data validation. But as soon as you get into business with more complex data, such as matching curly braces or comment blocks, the regular expression no longer does its job.

Here's a blog article about the limitations of using a regular expression to validate input.

To do this, you will have to write a parser that performs validation.

+1
source

A regular expression is not very convenient for such a task. This is often implemented using a stack with an algorithm similar to the following:

  • Create an empty stack S.
  • Bye (characters left) {
  • Read the ch character.
  • If ch is the starting groove (of any type), press it on S
  • Else
  • If ch is a closing pair (of any kind), look at the top of S.
  • If S is empty like this point, the report fails.
  • If the top of S is the initial guy that matches c, then pop S and continue with 1, this groove matches OK.
  • Refusal of the report.
  • If stack S is not empty at the end of input, return a failure. Else bring back success.

for more information check http://www.ccs.neu.edu/home/sbratus/com1101/lab4.html and http://codeidol.com/csharp/csharpckbk2/Data-Structures-and-Algorithms/Determining-Where -Characters-or-Strings-Do-Not-Balance /

+1
source

If you are trying to "check" the contents of a string defining a method, then you might be better off just using the CodeDom classes and compiling this method on the fly into a memory assembly.

Writing your own full-featured analyzer for validation will be very, very difficult, especially if you want to support C # 3 or later. Lambda expressions and other constructs like this will be very difficult to “verify” cleanly.

0
source

You draw a false dichotomy between “characters that will be invalid by parsing the rest of the code” and “syntax errors”. Missing a closing curly brace (one of the issues you mention) is a syntax error. Sounds like you mean you are looking for syntax errors that could potentially violate border boundaries? Unfortunately, there is no reliable way to do this without using a full parser.

As an example:

 method() { <-- is missing closing brace /* valid comment */ /* <-- bad for (i..) { } for (i..) { } <-- will be interpreted as the closing brace for the for loop 

There is no general, practical way to conclude that this is a for loop that does not have its closing bracket, and not a method.

If you are really interested in finding such things, you should consider starting the compiler programmatically and analyzing the results - the best approach with a minimal input threshold.

0
source

Source: https://habr.com/ru/post/1304508/


All Articles