简体   繁体   English

在多行 C-Functions 中解析 arguments

[英]Parsing arguments in multi-line C-Functions

I have a few examples of the first line of implementation of C functions that I need to extract the parameters from.我有几个需要从中提取参数的 C 函数的第一行实现示例。 For example:例如:

double FuncName1(char *testnum, char *hipin_source, char *hipin_sense,
                    char *lopin_source, char *lopin_sense, DMM_ResRange range, float delay) {

and

double FuncName2(char *testnum, char *hipin, char *lopin, DMM_ResRange range, float delay, bool offset) {

Is there any RegEx that I can use in C# to extract function name, return type, but more importantly the arguments?我可以在 C# 中使用任何正则表达式来提取 function 名称、返回类型,但更重要的是 arguments?

EDIT: I am developing in C# and need to parse C-source code files creating objects like Func that has string Name, string ReturnType, List with Arg object having string argType, and string argName编辑:我正在 C# 中开发,需要解析 C 源代码文件,创建具有字符串名称、字符串 ReturnType、带有 Arg object 的对象的对象,例如具有字符串 argType 和字符串 argName

EDIT 2: Well, I finally tried and it is not working... may be I did not define those right... I am trying to parse a function call that either coded like this:编辑 2:好吧,我终于尝试了,但它不起作用......可能是我没有定义正确的......我正在尝试解析一个 function 调用,它的编码如下:

     DCPS_Apply(1,    // Module (optional comment)
           5.0,  // Voltage (optional comment)
           2.0); // Current (optional comment)

or like this:或像这样:

     DCPS_Apply(1, 5.0, 2.0); // optional comment

and I need function name and the arguments...我需要 function 名称和 arguments...

Try following:尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                @"double FuncName1(char *testnum, char *hipin_source, char *hipin_sense,
                    char *lopin_source, char *lopin_sense, DMM_ResRange range, float delay) { }
                  double FuncName2(char *testnum, char *hipin, char *lopin, DMM_ResRange range, float delay, bool offset) { }
                ";

            string pattern1 = @"(?'type'\w+)\s+(?'name'[^(]+)\((?'parameters'[^)]+)\)";                    
            string pattern2 = @"(?'key'[^\s]+)\s+(?'value'[*\w]+),?";

            MatchCollection matches1 = Regex.Matches(input, pattern1);

            foreach (Match match1 in matches1.Cast<Match>())
            {
                string parameters = match1.Groups["parameters"].Value;
                MatchCollection matches2 = Regex.Matches(parameters, pattern2);
                string[] key_value = matches2.Cast<Match>().Select((x, i) => string.Format("Key[{0}] : '{1}', Value[{0}] : '{2}'", i, x.Groups["key"].Value, x.Groups["value"].Value)).ToArray();
                Console.WriteLine("Type : '{0}', Name : '{1}', Parameters : '{2}'", match1.Groups["type"].Value, match1.Groups["name"].Value, string.Join(";", key_value));
            }
            Console.ReadLine();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM