मैंने हाल ही में सी फाइलों को पार्स करने के लिए libclang का उपयोग करना शुरू कर दिया है। मेरी समस्या यह है कि स्पष्ट रूप से, libclang एएसटी उत्पन्न करने से पहले प्रीप्रोसेसर शुरू करता है। मैं चलने से पूर्वप्रक्रमक प्रतिबंधित करने के लिए, और बदले में जानकारी दी कि पूर्वप्रक्रमक निर्देशों फ़ाइल में हैं हो सकता है ...प्री-प्रोसेसर निर्देशों के बारे में जानकारी प्राप्त करें
मैं निम्नलिखित अजगर स्क्रिप्ट (cindex.py और libclang)
import codecs
from clang.cindex import *
class SourceFile(object):
def __init__(self, path):
with codecs.open(path, 'r', 'utf-8') as file:
self.file_content = file.read()
index = Index.create()
root_node = index.parse(path)
for included in root_node.get_includes():
print included.include
self.print_declerations(root_node.cursor)
def print_declerations(self, root, recurse=True):
print root.kind.name, root.spelling
if root.kind.is_declaration():
node_def = root.get_definition()
if node_def is not None:
start_offset = node_def.extent.start.offset
end_offset = node_def.extent.end.offset + 1
print self.file_content[start_offset:end_offset], '\n'
if recurse:
for child in root.get_children():
self.print_declerations(child, False)
if __name__ == '__main__':
path = 'Sample.cpp'
print 'Translation unit:', path
source = SourceFile(path)
का उपयोग करना चाहते हैं कौन सा आउटपुट
Translation unit: Sample.cpp
/mingw/include\stdio.h
/mingw/include\_mingw.h
/mingw/include\sys/types.h
TRANSLATION_UNIT None
TYPEDEF_DECL __builtin_va_list
STRUCT_DECL _iobuf
TYPEDEF_DECL FILE
VAR_DECL _iob
UNEXPOSED_DECL
FUNCTION_DECL main
int main()
{
printf(HELLO_WORLD);
return 0;
}
निम्नलिखित सी-कोड के लिए:
#include <stdio.h>
#define HELLO_WORLD "HELLO!"
int main()
{
printf(HELLO_WORLD);
return 0;
}
मैं चाहता हूं कि कोड में मेरे # परिभाषा के लिए DEFINE_DECL HELLO_WORLD प्राप्त करें (वर्तमान में मुझे कुछ भी नहीं मिलता है)। और निश्चित रूप से मेरे # शामिल के लिए समान बयान भी प्राप्त करें। क्या यह संभव है?
संपादित करें: असल में, मैं प्रीप्रोसेसर निर्देशों के बिना फ़ाइल को पार्स करना चाहता हूं।
आप के अलावा कुछ विचार करने के लिए तैयार हैं, तो Clang, मेरे पास एक वैकल्पिक समाधान है। –